PIL显示gif中的所有先前帧

6
pillow version:5.4.1

问题是我能看到gif中的所有帧。
期望:在一个有5个帧的gif的生命周期中,每个帧都应该可见一段时间,在gif的最后只应该可见第五帧。
实际情况:在我的情况下,在gif的最后我可以看到所有帧1-2-3-4-5。
gif动画中有5个帧,所有帧都是gif格式(静态),我也尝试过使用png文件,但结果相同。
我所有的帧都有透明背景。
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)

如果有人遇到了相同的问题,请让我知道我做错了什么?

enter image description here

使用下面的代码,使用imageio可以工作,但我失去了透明度,而且与PIL相比非常慢。
images = []
for filename in names:
    images.append(imageio.imread(filename))
imageio.mimsave('anitest.gif', images,duration=0.3)

可能是你的浏览器问题,尝试使用其他程序查看它... https://stackoverflow.com/a/40442936/2836621 - Mark Setchell
我没有在浏览器上检查过,但是在你的建议下我在浏览器上检查了一下,但是结果还是一样的。 - Soumyaansh
你能分享这个GIF吗? - Mark Setchell
已添加 GIF!我增加了循环次数,只是为了展示出问题出在哪里。 - Soumyaansh
我可以看出问题所在 - 它没有在帧之间清除,因此“处理”没有设置正确。我正在尝试使用 save(..., disposal=N) 进行实验。 - Mark Setchell
1个回答

13

我认为"处理"没有被正确地设置。请尝试使用以下方法:

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

enter image description here


disposal 参数的工作机制是什么? - Valentin Popescu
1
@ValentinPopescu 这里有一个一流的描述 https://legacy.imagemagick.org/Usage/anim_basics/ - Mark Setchell
1
@ValentinPopescu 更多信息请参见 https://legacy.imagemagick.org/Usage/anim_opt/ - Mark Setchell
1
@ValentinPopescu 更多的信息... https://legacy.imagemagick.org/Usage/anim_mods/ - Mark Setchell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接