Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

python - 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...