python - 使用 Python Wand 只播放一次 GIF

标签 python python-3.x gif animated-gif wand

我可以像这样创建一个动画 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())

但是,像这样创建的图像会无限循环。这一次,我希望它循环一次,然后停止。我知道我可以使用 convert-loop参数,如果我使用命令行界面。但是,我无法使用 Wand API 找到如何执行此操作。

我应该调用什么方法,或者我应该设置什么字段,以使生成的 GIF 循环恰好一次?

最佳答案

您需要使用 ctypes将魔杖库绑定(bind)到正确的 C-API 方法。
幸运的是,这很简单。

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())

关于python - 使用 Python Wand 只播放一次 GIF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51217433/

相关文章:

使用超时时 Python OpenSSL 连接握手失败

python - 使用 OpenCV 的 VideoCapture 方法进行高效链接处理? {编辑}

javascript - 将 JavaScript 对象/数组转换为 Python 字典/列表的最快方法

python - 如何连接同一数据帧中的 2 列,但从第 1 列获取行 a 到 d,从第 2 列获取 m 到 p?

python - shap.Explainer 构造函数错误要求未记录的位置参数

Django 密码重置生成 http 链接而不是 https

python - 使用类在python中进行异常处理

python - 为什么某些 GIF 动画会在 Pillow 中显示有问题的区域?

r - 将多个png转换为gif作为R中的动画

animation - Windows 8/Server 2012风格等待/加载透明GIF/APNG