2

I am trying to create and save a GIF from a set of PNG files.

pics=[]
for plot_path in plot_paths:
    img = Image.open(plot_path)
    pics.append(img)
pics[0].save(save_dir+'/truestrain.gif', format='gif', save_all=True, append_images=pics[1:], duration=10, loop=0)

The output is a gif file, of the correct name, but only using the first PNG file, and 10 seconds long.

save_all=True should prompt it to use all of the images in append_images=pics[1:], but that doesn't seem to be working.

duration=10 should set the duration between frames as 10ms, seems to be interpreted as total time 10s (contrary to the Pillow documentation?)

I have seen a relevant previous post, which agrees with the method I am using, but still having problems (Saving an animated GIF in Pillow). I've also checked this follows the documentation (https://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html).

finefoot
  • 9,914
  • 7
  • 59
  • 102
Joseph Langley
  • 115
  • 1
  • 9

2 Answers2

1

So it turns out, the GIF is being created correctly. Neither windows Photos or VLC was able to play it for some reason. I downloaded an alternative GIF viewer and the file is as expected.

Joseph Langley
  • 115
  • 1
  • 9
0

Your code works for me with your version 6.2.0. I just tested different settings for duration and can definitely see the difference:

from PIL import Image
assert Image.__version__ == "6.2.0"
frames = [Image.open(f"frame_{i:0>3}.png") for i in range(44)]
for duration in [10, 100, 1000]:
    frames[0].save(f"result_{duration}.gif", format="gif", save_all=True, append_images=frames[1:], duration=duration, loop=0)

There is a limit to duration precision of the GIF format itself. But it should be one hundredth of a second, so your duration=10 should be fine.

finefoot
  • 9,914
  • 7
  • 59
  • 102