python - 使用 Python Wand 使 GIF 只播放一次
问题描述
我可以像这样创建一个动画 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
's参数。-loop
但是,我无法找到如何使用 Wand API 执行此操作。
我应该调用什么方法,或者我应该设置什么字段,以使生成的 GIF 循环恰好一次?
解决方案
您需要使用ctypes
将 wand 库绑定到正确的 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())
推荐阅读
- scala - 在 shell 中运行 Scala 脚本时导入错误
- python - 计算数据框中每列值的订单百分比
- c - 包含的 C 文件中的编译指示
- amazon-web-services - 将客户端 VPN VPC 的流量路由到同一 VPC 中的实例
- c# - 如何在 .NET 中将 IEEE Single 转换为 MBF Single
- java - 在 Java 类 onClickListener() 和 switch(v.getId) 中需要帮助
- node.js - 即使已指定值,为什么 Apollo 客户端仍将空值插入所有字段的表中?
- regex - 如果以逗号结尾,如何从 Google 表格单元格末尾删除逗号?
- video - ffmpeg 输出 25fps 视频的第一帧仅显示 1ms
- python - 如何让 matplotlib (pyplot) 图循环更新?