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)
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.
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.
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.
Or you could just call it like that:
imageio.mimsave(exportname, frames, format='GIF', duration=5)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With