[英]Extract key frames from GIF using Python
我想通过从最好应该是不同的 GIF 中提取 15 帧来压缩 GIF 图像。
我正在使用 Python 和 Pillow 库,但我没有找到任何方法来获取Pillow 文档中GIF 的帧数。 我也没有找到如何从 GIF 中提取特定帧,因为Pillow 限制了.
有没有办法提取帧而不需要遍历每个帧? 是否有更高级的用于 GIF 处理的 Python 库?
这是@ radarhere的答案的扩展,它将.gif分成num_key_frames
不同的部分,并将每个部分保存到新图像。
from PIL import Image
num_key_frames = 8
with Image.open('somegif.gif') as im:
for i in range(num_key_frames):
im.seek(im.n_frames // num_key_frames * i)
im.save('{}.png'.format(i))
结果是somegif.gif
分为8个保存为0
.. 7.png
。
对于帧数,您正在寻找n_frames - https://pillow.readthedocs.io/en/5.2.x/reference/plugins.html#PIL.GifImagePlugin.GifImageFile.n_frames 。
from PIL import Image
im = Image.open('test.gif')
print("Number of frames: "+str(im.n_frames))
用于提取单个帧 -
im.seek(20)
im.save('frame20.gif')
提取任何 GIF 文件的正确帧的真正可行的解决方案:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.