Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PIL is showing all the previous frames in the gif

pillow version:5.4.1

The problem is I can see all the frames in the gif.

Expectation: in the lifecycle of a gif having 5 frames each frames should be visible for a fraction of time and at the end of gif only 5th frame should be visible.

Actual: In my case at the end of the gif I can see all the frames 1-2-3-4-5.

There are 5 frames in the gif animation and all the frames are in gif format(static) I also tried with png files too but I am getting the same result.

All my frames are with transparent background.

from PIL import Image

frame_list = []
frame_list.append("object_1.gif")
frame_list.append("object_2.gif")
frame_list.append("object_3.gif")
frame_list.append("object_4.gif")
frame_list.append("object_5.gif")

images = []
for n in frame_list:
    frame = Image.open(n)
    images.append(frame)

images[0].save('anitest.gif',
               save_all=True,
               format='GIF',
               append_images=images[1:],
               duration=200,
               loop=0)

If anyone encountered the same issue please let me know what am I doing wrong?

enter image description here

With the below code using imageio it's working but I am loosing transparency and its very slow compare PIL

images = []
for filename in names:
    images.append(imageio.imread(filename))
imageio.mimsave('anitest.gif', images,duration=0.3)
like image 544
Soumyaansh Avatar asked Jan 26 '23 13:01

Soumyaansh


1 Answers

I think the "disposal" is not being set correctly. Try with:

images[0].save('anitest.gif',
               save_all=True,
               format='GIF',
               append_images=images[1:],
               duration=200,disposal=2,
               loop=0)

enter image description here

like image 83
Mark Setchell Avatar answered Feb 08 '23 12:02

Mark Setchell