【发布时间】:2016-07-18 09:42:42
【问题描述】:
我一直在玩 Python 中的动画 gif,其中帧将由位于温室中的 Raspberry Pi 相机生成。我已经成功地使用了来自Almar's answer to a previous question 的推荐 imageio 代码来创建简单的 gif。
但是,我现在正试图减慢帧持续时间,但查看 documentation for imageio 并找不到任何有关 mimsave 的引用,但确实看到了 mimwrite,这应该需要四个参数。我查看了additional gif documentation,可以看到有一个持续时间参数。
目前,我的代码如下:
exportname = "output.gif"
kargs = { 'duration': 5 }
imageio.mimsave(exportname, frames, 'GIF', kargs)
我收到以下错误:
Traceback (most recent call last):
File "makegif.py", line 23, in <module>
imageio.mimsave(exportname, frames, 'GIF', kargs)
TypeError: mimwrite() takes at most 3 arguments (4 given)
其中 frames 是 imageio.imread 对象的列表。这是为什么呢?
已更新以显示完整答案: 这是一个示例,展示了如何使用 imageio 创建动画 gif,使用 kwargs 来更改帧持续时间。
import imageio
import os
import sys
if len(sys.argv) < 2:
print("Not enough args - add the full path")
indir = sys.argv[1]
frames = []
# Load each file into a list
for root, dirs, filenames in os.walk(indir):
for filename in filenames:
if filename.endswith(".jpg"):
print(filename)
frames.append(imageio.imread(indir + "/" + filename))
# Save them as frames into a gif
exportname = "output.gif"
kargs = { 'duration': 5 }
imageio.mimsave(exportname, frames, 'GIF', **kargs)
【问题讨论】: