Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I have used the recommended imageio code from Almar's answer to a previous question with success to create simple gifs.

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. I've looked at the additional gif documentation and can see that there is a duration argument.

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

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)
like image 276
Byte Insight Avatar asked Jul 18 '16 09:07

Byte Insight


People also ask

How many seconds per frame is a GIF?

Standard GIFs run between 15 and 24 frames per second. Overall, the smaller your GIF file size, the lower the quality will be. When creating GIFs for the web, it is all about finding the smallest file size possible without sacrificing too much quality.

What is frame delay in GIFs?

23.5. 1. Frame Delay Also called "interframe delay," this setting specifies the amount of time between frames. Frame delays are measured in 1/100ths of a second. You can apply a different delay time to each frame in the animation to create pauses and other timing effects.

How many frames does it take to make a smooth GIF?

The key to the smoothness is the GIF frame delay time. Most computer displays run at 60fps (frames per second), so the best frame rates for smooth animation are 60fps and 30fps, or even 15fps if the motion is slow and you're trying to create the smallest file possible.


1 Answers

Or you could just call it like that:

imageio.mimsave(exportname, frames, format='GIF', duration=5)
like image 50
Gwen Avatar answered Sep 17 '22 09:09

Gwen