使用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");
示例输出:
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
15分钟后,第五个循环尚未完成,手动终止。在此期间,目标文件(nowplaying.gif)继续增长,进程终止时大小接近2 MB。最后的gif被破坏,这是有道理的,因为它没有完全保存
我不确定我是否做错了什么,或者我在魔杖实现(版本0.6.1,最新版本)中偶然发现了一个bug。我的Python技能不是很好,但我已经仔细阅读了bug报告和文档,在这个特定问题上我什么也看不到
问题似乎与过时的魔杖版本有关;所使用的python解释器运行的是0.5.8,而不是0.6.1(我有两个python安装,使用pip引用了错误的一个)。更新到0.6.1解决了这个问题,可能是内存引用重写的一部分
考虑到这个问题,答案是肯定的
相关问题 更多 >
编程相关推荐