使用 Python 从 GIF 中提取关键帧 [英] Extract key frames from GIF using Python

查看:26
本文介绍了使用 Python 从 GIF 中提取关键帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过从 GIF 中提取 15 帧(最好应该是不同的)来压缩 GIF 图像.

I want to compress a GIF image by extracting 15 frames from the GIF that preferably should be distinct.

我正在使用 Python 和 Pillow 库,但在 枕头文档.我也没有找到如何从 GIF 中提取特定帧,因为 枕头限制.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆