简体   繁体   English

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

[英]Custom Frame Duration for Animated Gif in Python ImageIO

I've been playing around with animated gifs in Python, where the frames will be produced by a Raspberry Pi Camera located in a greenhouse. 我一直在玩Python中的GIF动画,框架将由位于温室中的Raspberry Pi相机生成。 I have used the recommended imageio code from Almar's answer to a previous question with success to create simple gifs. 我使用了Almar对前一个问题的回答推荐的imageio代码,成功创建了简单的GIF。

However, I'm now trying to slow down the frame duration but looking at the documentation for imageio and cannot find any references for mimsave but do see mimwrite , which should take four args. 但是,我现在正试图减慢帧持续时间但是查看imageio文档并且找不到mimsave的任何引用但是看到mimwrite ,它应该采用四个args。 I've looked at the additional gif documentation and can see that there is a duration argument. 我查看了额外的gif文档 ,可以看到有一个持续时间参数。

Currently, my code looks like: 目前,我的代码如下:

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

and I'm getting the following error: 我收到以下错误:

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)

where frames is a list of imageio.imread objects. 其中frames是imageio.imread对象的列表。 Why is this? 为什么是这样?

UPDATED TO SHOW FULL ANSWER: This is an example showing how to created animated gifs with imageio using kwargs to change the frame duration. 更新显示完整答案:这是一个示例,显示如何使用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 does not accept 4 positional arguments. mimsave不接受4个位置参数。 Anything beyond the 3rd argument must be provided as a keyword argument . 超出第三个参数的任何内容都必须作为关键字参数提供 In other words, you have to unpack kargs like so: 换句话说,你必须像这样解压缩kargs

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

或者你可以这样称呼它:

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

I've found this to be the simplest and most robust solution 我发现这是最简单,最强大的解决方案

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))

and then this last line of the script is the one you are looking for 然后脚本的最后一行就是你要找的那一行

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM