簡體   English   中英

如果多次調用 Python/Wand image.save() 會掛起

[英]Python/Wand image.save() hangs if called several times

使用 Python 代碼讀取周期性更改文件的內容(MP3 上的信息)並使用 Wand 根據手頭的信息創建特定的動畫 gif。 然而,我遇到了一個嚴重的問題,即 gif 在幾次通過后拒絕保存; 在兩到三遍之后,image.save() 要么無限運行,要么處理時間急劇增加。 我看不出突然變化的原因。

這里的代碼是一個測試用例,用於驗證問題不在於文件讀取,而事實並非如此。

# coding=latin-1

from wand.image import Image        # ImageMagick
from wand.font import Font          # IM Font Handler
from wand.drawing import Drawing    # IM Drawing API
from wand.color import Color        # IM Colour definitions
from watchdog.observers import Observer             # Event monitors
from watchdog.events import FileSystemEventHandler  # Event handlers

import time                         # for the stopwatch
import sys, os

FONT_NAME =  'c://windows//fonts//alarm clock.ttf';
FONT_SIZE = 80;
SPACING = 1;

def ImageMaker(title, album, composer, playtime):
    print("DEBUG: Entering ImageMaker()");
    start = time.time();

    title = title.ljust(60,' '); # these strings are space-padded to work properly in draw.text()
    album = album.ljust(60,' ');
    composer = composer.ljust(60,' ');
    details = [title, album, composer];

    counter = 0;
    frame_counter = 0;

    bg = Image(filename="nowplaying_background.png");   # this is a static, pre-generated file
    gif = Image(background=Color('rgb(0,0,0)'), height=200, width=1440);

    if (int(playtime) > 45):    # the script takes so long, very short MP3s finish before the script does
        for x in details:
            while (counter <= len(x)+1 and counter < 30 and len(x) != 0):
                with Image(height=200, width=1440, depth=8) as img:
                    with Drawing() as draw:
                        draw.font = FONT_NAME;
                        draw.font_size = FONT_SIZE;
                        draw.text_interline_spacing = SPACING;
                        draw.fill_color = Color('rgb(255, 194, 0)');
                        draw.stroke_width = 0;
                        a = x[(29-counter):30];     # start at 30, step back to 0
                        b = x[59-counter:len(x)];   # start at 60, step back to 30
                        draw.text(20, 65, a);   # top line text
                        draw.text(20, 130, b);  # bottom line text
                        draw(img);
                    with Image(height=200, width=1440) as final:
                        final.composite(bg);        # draw static bg on the image context
                        final.composite(img);       # draw the current fg over the bg
                        if(frame_counter > 0):
                            gif.sequence.append(final);
                        else:
                            gif.sequence[0] = final;
                        final.clear();
                        gif.sequence[frame_counter].delay = 30;
                        img.clear();
                counter += 1; 
                frame_counter += 1;
            counter = 0;
            print("DEBUG: Exiting frame generator");
        gif.sequence[29].delay = 200;       # these set the delays between the three elements longer
        gif.sequence[59].delay = 200;       # so they're more readable
        gif.sequence[89].delay = 200;
        gif.type = 'optimize';              
        gif.save(filename="c://users/magni/desktop/nowplaying.gif");
        gif.destroy();
        bg.destroy();
    else:
        print("DEBUG: Skipping image generation, song is too short");

    end = time.time();
    elapsed = end - start;
    print("Time to completition: ", elapsed);

# These are simulated samples
ImageMaker("ABCDEF", "GHIJKLMNO", "QRSTUVWXYZ", "60");
ImageMaker("ABCDEF", "GHIJKLMNO", "QRSTUVWXYZ", "60");
ImageMaker("QRSTUVWXYZ", "ABCDEF", "GHIJKLMNO", "90");
ImageMaker("QRSTUVWXYZ", "ABCDEF", "GHIJKLMNO", "90");
ImageMaker("American Meeting", "Teitoku no Ketsudan 2", "Yoichiro Yoshikawa", "133");
ImageMaker("American Meeting", "Teitoku no Ketsudan 2", "Yoichiro Yoshikawa", "133");

示例 output:

DEBUG: Entering ImageMaker()
DEBUG: Exiting frame generator
DEBUG: Exiting frame generator
DEBUG: Exiting frame generator
Time to completition:  22.29582381248474
DEBUG: Entering ImageMaker()
DEBUG: Exiting frame generator
DEBUG: Exiting frame generator
DEBUG: Exiting frame generator
Time to completition:  23.027299642562866
DEBUG: Entering ImageMaker()
DEBUG: Exiting frame generator
DEBUG: Exiting frame generator
DEBUG: Exiting frame generator
Time to completition:  21.273354291915894
DEBUG: Entering ImageMaker() on a modified file
DEBUG: Exiting frame generator
DEBUG: Exiting frame generator
DEBUG: Exiting frame generator
Time to completition:  139.14677023887634
DEBUG: Entering ImageMaker()
DEBUG: Exiting frame generator
DEBUG: Exiting frame generator
DEBUG: Exiting frame generator

十五分鍾后,第五個循環還沒有結束,被手動終止了。 目標文件 (nowplaying.gif) 在此期間繼續增長,當進程被終止時,其大小接近 2 兆字節。 最后的 gif 被破壞了,這是有道理的,因為它沒有完全保存。

我不確定我是否做錯了什么,或者我偶然發現了 Wand 實現中的一個錯誤(版本 0.6.1,最新可用)。 我的 Python 技能不是特別好,但我仔細閱讀了錯誤報告和文檔,在這個特定問題上我什么也沒看到。

問題似乎與過時的 Wand 版本有關; 正在使用的 python 解釋器運行的是 0.5.8,而不是 0.6.1(我有兩個 python 安裝,並且用 pip 引用了錯誤的一個)。 更新到 0.6.1 解決了這個問題,可能是 memory 引用重寫的一部分。

考慮到這個問題得到了回答。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM