Python 在 Pillow 中保存动画 GIF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24688802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:01:32  来源:igfitidea点击:

Saving an animated GIF in Pillow

pythonpillow

提问by leewz

(Python 3.4, PIL 1.1.7, Pillow 2.5.1)

(Python 3.4,PIL 1.1.7,枕头 2.5.1)

I expected this to copy the original GIF.

我希望这会复制原始 GIF。

from PIL import Image
im = Image.open(filename)
im.save('temp.gif')

Instead, it saves the first frame as a still.

相反,它将第一帧保存为静止图像。

What am I doing wrong?

我究竟做错了什么?

采纳答案by blakev

Use the script found on the Pillow Github, here.

使用 Pillow Github 上的脚本,这里

 import ImageSequence
 import Image
 import gifmaker
 sequence = []

 im = Image.open(....)

 # im is your original image
 frames = [frame.copy() for frame in ImageSequence.Iterator(im)]

 # write GIF animation
 fp = open("out.gif", "wb")
 gifmaker.makedelta(fp, frames)
 fp.close()

回答by mr. Y

One can see that the new version of gifmaker script simply uses save method with special kwargs for GIF.

可以看到新版本的 gifmaker 脚本简单地使用了带有特殊 kwargs 的保存方法用于 GIF。

As the documentation states (https://pillow.readthedocs.org/en/latest/handbook/image-file-formats.html#saving-sequences):

正如文档所述(https://pillow.readthedocs.org/en/latest/handbook/image-file-formats.html#saving-sequences):

When calling save(), if a multiframe image is used, by default only the first frame will be saved. To save all frames, the save_all parameter must be present and set to True.

If present, the loop parameter can be used to set the number of times the GIF should loop, and the duration parameter can set the number of milliseconds between each frame.

调用 save() 时,如果使用多帧图像,默认情况下只会保存第一帧。要保存所有帧,必须存在 save_all 参数并将其设置为 True。

如果存在,loop 参数可用于设置 GIF 应循环的次数,duration 参数可设置每帧之间的毫秒数。

回答by reducing activity

Version that requires only pillow and works:

只需要枕头和工作的版本:

from PIL import Image

width = 300
height = 300
im1 = Image.new("RGBA", (width, height), (255, 0, 0))
im2 = Image.new("RGBA", (width, height), (255, 255, 0))
im3 = Image.new("RGBA", (width, height), (255, 255, 255))
im1.save("out.gif", save_all=True, append_images=[im2, im3], duration=100, loop=0)

using existing images:

使用现有图像:

from PIL import Image

im1 = Image.open('a.png')
im2 = Image.open('b.png')
im3 = Image.open('c.png')
im1.save("out.gif", save_all=True, append_images=[im2, im3], duration=100, loop=0)

And, as too low versions of pillow are silently failing here is as a bonus version with a library version check:

而且,由于枕头版本太低,这里无声无息地失败了,作为带有库版本检查的奖励版本:

from packaging import version
from PIL import Image

im1 = Image.open('a.png')
im2 = Image.open('b.png')
im3 = Image.open('c.png')
if version.parse(Image.PILLOW_VERSION) < version.parse("3.4"):
    print("Pillow in version not supporting making animated gifs")
    print("you need to upgrade library version")
    print("see release notes in")
    print("https://pillow.readthedocs.io/en/latest/releasenotes/3.4.0.html#append-images-to-gif")
else:
    im1.save("out.gif", save_all=True, append_images=[
             im2, im3], duration=100, loop=0)