簡體   English   中英

Python ImageIO中動畫Gif的自定義幀持續時間

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM