Python ImageIO中针对动态GIF的自定义帧持续时间

24

我一直在使用Python中的动态gif图像,其中的帧是由位于温室中的Raspberry Pi摄像头产生的。我成功地使用了推荐的imageio代码来创建简单的gif图像,该代码来自于Almar曾经回答的问题

然而,我现在想减慢帧速率,查看了imageio文档,没有找到有关mimsave的任何参考资料,但我看到了mimwrite,它应该需要四个参数。我查看了其他gif文档,发现有一个duration参数。

目前,我的代码如下:

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)
3个回答

27

或者你可以这样称呼它:

imageio.mimsave(exportname, frames, format='GIF', duration=5)

13
在'imageio'版本为'2.5.0'中,我们需要直接将帧速率作为'fps'参数传递,而不是持续时间。imageio.imsave(exportname, frames, format='GIF', fps=30) - Ernest S Kirubakaran

14

mimsave 不接受 4 个 位置参数。第三个参数之后的任何内容都必须作为 关键字参数 提供。换句话说,您必须像这样解包 kargs

imageio.mimsave(exportname, frames, 'GIF', **kargs)

7
我发现这是最简单且最稳健的解决方案。
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接