[英]Custom Frame Duration for Animated Gif in Python ImageIO
我一直在玩Python中的GIF动画,框架将由位于温室中的Raspberry Pi相机生成。 我使用了Almar对前一个问题的回答推荐的imageio代码,成功创建了简单的GIF。
但是,我现在正试图减慢帧持续时间但是查看imageio的文档并且找不到mimsave的任何引用但是看到mimwrite ,它应该采用四个args。 我查看了额外的gif文档 ,可以看到有一个持续时间参数。
目前,我的代码如下:
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对象的列表。 为什么是这样?
更新显示完整答案:这是一个示例,显示如何使用kwargs创建带有imageio的GIF动画来更改帧持续时间。
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)
mimsave
不接受4个位置参数。 超出第三个参数的任何内容都必须作为关键字参数提供 。 换句话说,你必须像这样解压缩kargs
:
imageio.mimsave(exportname, frames, 'GIF', **kargs)
或者你可以这样称呼它:
imageio.mimsave(exportname, frames, format='GIF', duration=5)
我发现这是最简单,最强大的解决方案
import imageio
import os
path = '/path/to/script/and/frames'
image_folder = os.fsencode(path)
filenames = []
for file in os.listdir(image_folder):
filename = os.fsdecode(file)
if filename.endswith( ('.jpeg', '.png', '.gif') ):
filenames.append(filename)
filenames.sort() # this iteration technique has no built in order, so sort the frames
images = list(map(lambda filename: imageio.imread(filename), filenames))
然后脚本的最后一行就是你要找的那一行
imageio.mimsave(os.path.join('my_very_own_gif.gif'), images, duration = 0.04) # modify the frame duration as needed
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.