Python ImageIO中动画Gif的自定义帧持续时间

Dav*_*ard 8 python animation image

我一直在玩Python中的GIF动画,框架将由位于温室中的Raspberry Pi相机生成.我使用了Almar对前一个问题的回答推荐的imageio代码,成功创建了简单的GIF.

但是,我现在正试图减慢帧持续时间但是查看imageio文档并且找不到mimsave的任何引用但是看到mimwrite,它应该采用四个args.我查看了额外的gif文档,可以看到有一个持续时间参数.

目前,我的代码如下:

exportname = "output.gif"
kargs = { 'duration': 5 }
imageio.mimsave(exportname, frames, 'GIF', kargs)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

其中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)
Run Code Online (Sandbox Code Playgroud)

Ara*_*Fey 9

mimsave不接受4个位置参数.超出第三个参数的任何内容都必须作为关键字参数提供.换句话说,你必须kargs像这样解压缩:

imageio.mimsave(exportname, frames, 'GIF', **kargs)
Run Code Online (Sandbox Code Playgroud)


Gwe*_*wen 8

或者你可以这样称呼它:

imageio.mimsave(exportname, frames, format='GIF', duration=5)
Run Code Online (Sandbox Code Playgroud)

  • 在 imageio 版本“2.5.0”中,我们必须直接将 FPS 作为“fps”参数而不是持续时间。`imageio.imsave(导出名称,帧,格式='GIF',fps=30`) (6认同)

Dan*_*ath 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))
Run Code Online (Sandbox Code Playgroud)

然后脚本的最后一行就是您要查找的行

imageio.mimsave(os.path.join('my_very_own_gif.gif'), images, duration = 0.04) # modify the frame duration as needed
Run Code Online (Sandbox Code Playgroud)