40

I have tried reading a lot of examples online and found imageio is the perfect package for it. Also found examples written in here.

I have just followed the example as shown and tried the following

import imageio as io
import os
file_names = sorted((fn for fn in os.listdir('.') if fn.startswith('surface')))
#making animation
with io.get_writer('surface.gif', mode='I', duration=0.5) as writer:
    for filename in file_names:
        image = io.imread(filename)
        writer.append_data(image)
writer.close()

and another example.

images = []
for filename in file_names:
    images.append(io.imread(filename))
io.mimsave('surface1.gif', images, duration = 0.5)

both of these do not work. And basically i only see the first frame from the gif and a blink and finish. The duration is set 0.5secs, so it should work fine. I might have been missing out something here.

4
  • 1
    Both approaches work on Python 2.7.1 and ´imageio´ 2.1.2 (1 2), using several .png images. Are you sure all the images you want to use are being selected? Check the file_names list to see if they are all there.
    – berna1111
    Commented Mar 28, 2017 at 10:18
  • I have tried the first approach. it works for about 758 .png files. But I have around 3000 .png files and the files after 758 are not appended in the .gif file. Is there any reason for that? I know the file_names array has those files. Refer stackoverflow.com/questions/44650649/… for more on my question. Thanks!!
    – user77005
    Commented Jun 21, 2017 at 2:17
  • 1
    Check out quora.com/How-long-can-a-GIF-animate Commented Jul 23, 2017 at 2:28
  • duration should be set in mili seconds and not seconds.
    – Shaq
    Commented Oct 2, 2023 at 14:02

2 Answers 2

44

This works for me:

import os
import imageio

png_dir = '../animation/png'
images = []
for file_name in sorted(os.listdir(png_dir)):
    if file_name.endswith('.png'):
        file_path = os.path.join(png_dir, file_name)
        images.append(imageio.imread(file_path))

# Make it pause at the end so that the viewers can ponder
for _ in range(10):
    images.append(imageio.imread(file_path))

imageio.mimsave('../animation/gif/movie.gif', images)
6
  • This is the most easy and elegant solution I've found so far. One suggestion, add the sorted function before the list_dir function: sorted(os.listdir(png_dir))
    – Comos
    Commented Feb 12, 2021 at 14:38
  • I'm trying this solution, but the issue here is image transparency, while creating GIF some image color changed Commented Jul 23, 2021 at 11:21
  • 1
    @DaveBabbitt I am using this solution but facing color change issue in the image. Please use PIL but only one thing remember 1 second==100 mili second. Commented Jul 30, 2021 at 10:47
  • 1
    How can we slow down the gif? Commented Mar 31, 2022 at 9:22
  • 1
    imageio.mimsave has a duration= parameter in seconds Commented Apr 1, 2022 at 11:22
30

in case anyone needs it, for python 3.6.8, it needed fps

imageio.mimsave('/path/file.gif',images,fps=55)
2
  • it does not support fps anymore, what version of imageio is this so that i can use it?
    – vs07
    Commented May 18, 2023 at 23:46
  • 24/01/24: it works again exactly as provided here Commented Jan 24 at 13:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.