0

I'm trying to combine many png images into a gif file using this solution, but it only saves one png in the gif. What am I doing wrong? My MWE is

from PIL import Image, ImageDraw
import matplotlib.pyplot as plt
import numpy as np
import glob

thist = 4 # total time
tstep = 0 # time step

while thist>=tstep:

    x = np.linspace(0,2,100)
    y = np.sin(x*(thist-tstep)) # complicated calculation

    fig = plt.figure(figsize=(10,6))
    ax = plt.subplot(111)

    plt.plot(x,y)
    plt.savefig('mytest'+str(tstep)+'.png')

    tstep += 1

fp_in = "mytest*.png"
fp_out = "myimage.gif"

# https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#gif
img, *imgs = [Image.open(f) for f in sorted(glob.glob(fp_in))]
img.save(fp=fp_out, format='GIF', append_images=imgs,
         save_all=True, duration=200, loop=0)

In addition,

>> print(glob.glob(fp_in))

['mytest0.png', 'mytest1.png', 'mytest2.png', 'mytest3.png', 'mytest4.png']

>> print(imgs)

[<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1000x600 at 0x7F8FCEB33240>, <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1000x600 at 0x7F8FCEB33400>, <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1000x600 at 0x7F8FCEB33470>, <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1000x600 at 0x7F8FCEBACB38>]
Medulla Oblongata
  • 3,771
  • 8
  • 36
  • 75
  • first you could check what you ge with `glob.glob(fp_in)` and what you get in `imgs`. – furas May 20 '21 at 02:03
  • it looks like both `glob.glob(fp_in)` and `imgs` contain the correct number of images – Medulla Oblongata May 20 '21 at 02:05
  • shorter and more readable: `for tstep in range(thist):` and it doesn't need `tstep += 1` – furas May 20 '21 at 02:05
  • your code gives me animated GIF. But not all programs display animated GIFs. I tested it in web browser Firefox. – furas May 20 '21 at 02:09
  • that's strange - it doesn't work in any of my browsers or apps. I'm using jupyter notebook 5.6.0, Python 3.5.2 – Medulla Oblongata May 20 '21 at 02:12
  • did you try to run it without Jupyter? Or maybe you should use newer Python with newest module `pillow`. – furas May 20 '21 at 02:16
  • normally I would use in console program [imagemagick](https://imagemagick.org/) instead of `python` to generate `gif` - something like `convert test*.png myimage.gif`. OR I would run it in Python with subprocess. `imagemgick` is also used by Python module [Wand](https://docs.wand-py.org/en/0.6.6/) – furas May 20 '21 at 02:20
  • it works outside of jupyter. but I must use jupyter to do my data processing - this MWE shows that it would be a problem for generating gifs (no I'm not allowed to install imagemagick or wand) – Medulla Oblongata May 20 '21 at 02:21

1 Answers1

1

As others have mentioned in comments the code you posted works on the latest Python + pillow distribution. Possible issue with your pillow version, similar issue is mentioned here PIL.PngImagePlugin.PngImageFile images can't be saved as GIFs in 7.1.1.

Solution in that issue is to

  1. update Pillow
  2. .copy() all frames
  3. use jpg instead of png.