[英]Make a GIF play exactly once with Python Wand
I can create an animated GIF like this: 我可以这样创建动画GIF:
from wand.image import Image
with Image() as im:
while i_need_to_add_more_frames():
im.sequence.append(Image(blob=get_frame_data(), format='png'))
with im.sequence[-1] as frame:
frame.delay = calculate_how_long_this_frame_should_be_visible()
im.type = 'optimize'
im.format = 'gif'
do_something_with(im.make_blob())
However, an image created like this loops indefinitely. 但是,像这样创建的图像会无限循环。 This time, I want it to loop once, and then stop. 这次,我希望它循环一次,然后停止。 I know that I could use convert
's -loop
parameter if I were using the commandline interface. 我知道如果我使用命令行界面,则可以使用convert
的-loop
参数。 However, I was unable to find how to do this using the Wand API. 但是,我找不到使用Wand API的方法。
What method should I call, or what field should I set, to make the generated GIF loop exactly once? 我应该调用什么方法,或者应该设置什么字段来使生成的GIF循环恰好一次?
You'll need to use ctypes
to bind the wand library to the correct C-API method. 您需要使用ctypes
将魔杖库绑定到正确的C-API方法。 Luckily this is straightforward. 幸运的是,这很简单。
import ctypes
from wand.image import Image
from wand.api import library
# Tell Python about the C-API method.
library.MagickSetImageIterations.argtypes = (ctypes.c_void_p, ctypes.c_size_t)
with Image() as im:
while i_need_to_add_more_frames():
im.sequence.append(Image(blob=get_frame_data(), format='png'))
with im.sequence[-1] as frame:
frame.delay = calculate_how_long_this_frame_should_be_visible()
im.type = 'optimize'
im.format = 'gif'
# Set the total iterations of the animation.
library.MagickSetImageIterations(im.wand, 1)
do_something_with(im.make_blob())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.