使用Python从GIF提取关键帧 [英] Extract key frames from GIF using Python
问题描述
我想通过从GIF中提取15个最好是不同的帧来压缩GIF图像.
I want to compress a GIF image by extracting 15 frames from the GIF that preferably should be distinct.
我正在使用Python和Pillow库,但在枕头对此进行了限制.
I'm using Python and Pillow library and I didn't find any way to get the number of frames a GIF has in the Pillow docs. Neither did I find how to extract a specific frame from a GIF, because Pillow restricts that.
是否有任何方法可以提取帧而无需遍历每个帧?是否有用于GIF处理的更高级的Python库?
Is there any way to extract frames without iterating through each frame consequently? Is there a more advanced Python library for GIF processing?
推荐答案
这里是@radarhere答案的扩展,它将.gif分为 num_key_frames
个不同的部分,并将每个部分保存到新图像中.
Here is an extension of @radarhere's answer that divides the .gif into num_key_frames
different parts and saves each part to a new image.
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
.
The result is somegif.gif
broken into 8 pieces saved as 0
..7.png
.
这篇关于使用Python从GIF提取关键帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!