1

I'm trying to make GIF from multiple image files, following this thread.

Kris's answer works perfectly when I open a physical file from a directory using Image.open() and then append the list to make a GIF.

imgs = [Image.open(f) for f in os.listdir(os.getcwd())]
imgs[0].save('{}.gif'.format(img_filename),
                              format='GIF',
                              append_images=imgs[1:],
                              save_all=True,
                              duration=args.duration,
                              loop=0)

This works fine and the output GIF is infact animated. I noticed that the list glitched_imgs consists of PIL.PngImagePlugin.PngImageFile objects in this case.

However whenever I try to use an Image object returned from a function, The GIF produced is completely static.

imgs = [get_randomized_image() for _ in range(args.frames)]
imgs[0].save('{}.gif'.format(img_filename),
                              format='GIF',
                              append_images=imgs[1:],
                              save_all=True,
                              duration=args.duration,
                              loop=0)

Where get_randomized_image() returns a randomized Image object, specifically from a numpy array.

    # Inside `get_glitched_image()`
    return Image.fromarray(outputarr, img_mode)

I want to directly make a GIF with a list of objects returned from this function, how can I achieve this?

Note: The files I open in the first (working) method, are the files I saved prior using get_randomized_images() so I can assure you they are exactly the same.

MeiH
  • 1,763
  • 11
  • 17
Chase
  • 5,315
  • 2
  • 15
  • 41
  • Did you check how many images are in your randomised list? And print them to see their sizes and modes to ensure they match? – Mark Setchell Feb 20 '20 at 12:47
  • Maybe ensure they are all RGBA by changing to `return Image.fromarray(...).convert('RGBA')` – Mark Setchell Feb 20 '20 at 12:59
  • @MarkSetchell I forgot to mention, so I added an edit. The images I used in the first (working) method were created by using `.save()` on the returned `Image` object from the same function - `get_randomized_images()`. I just open those same files in the first (working) method and directly use the `Image` object in the second. So in both methods, the data is exactly the same format – Chase Feb 20 '20 at 13:10
  • @MarkSetchell Also yes, the length of the list is the same (23 for this specific use case) in both methods – Chase Feb 20 '20 at 13:12
  • If you open a PNG file, convert it to a Numpy array and then convert back to PIL Image and save, you will not necessarily get a similar file. – Mark Setchell Feb 20 '20 at 13:14
  • I see, is there perhaps a way to turn `Image` into `PIL.PngImagePlugin.PngImageFile` without having to save and reopen? – Chase Feb 20 '20 at 13:40

0 Answers0