Saving an animated GIF in Pillow

2019-01-24 11:11发布

(Python 3.4, PIL 1.1.7, Pillow 2.5.1)

I expected this to copy the original 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?

标签: python pillow
2条回答
我想做一个坏孩纸
2楼-- · 2019-01-24 11:39

Use the script found on the Pillow Github, here.

 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()
查看更多
我只想做你的唯一
3楼-- · 2019-01-24 11:40

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

As the documentation states (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.

查看更多
登录 后发表回答