从GIF提取帧时出现更多问题 [英] More problems extracting frames from GIFs

查看:126
本文介绍了从GIF提取帧时出现更多问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的上一个问题(用python打开的Gif框架损坏了)现在我的代码有时可以正常工作.

Following my previous question (Gifs opened with python have broken frames) I now have code that works sometimes.

例如,此代码

from PIL import Image

img = Image.open('pigs.gif')

counter = 0
collection = []
current = img.convert('RGBA')
while True:
    try:
        current.save('original%d.png' % counter, 'PNG')
        img.seek(img.tell()+1)
        current = Image.alpha_composite(current, img.convert('RGBA'))
        counter += 1
    except EOFError:
        break

…可以完美地在大多数GIF上工作,但在其他GIF上却产生奇怪的结果.例如,当应用于此2帧GIF时:

…works on most GIFs perfectly, but on others it produces weird results. For example, when applied to this 2-frame GIF:

它产生以下两个帧:

第一个还可以,第二个就不行了.

The first one is ok, the second one not so much.

现在怎么办?

推荐答案

尝试魔杖(Wand是用于Python的基于ctypes的简单ImageMagick绑定.)

try Wand (Wand is a ctypes-based simple ImageMagick binding for Python.)

from wand.image import Image

def extract_frames(gif_file):
    with Image(filename=gif_file) as gif:
        for i, single_img in enumerate(gif.sequence):
            with Image(image=single_img) as frame:
                frame.format = 'PNG'
                frame.save(filename='frame-%d.png' % i)

extract_frames('test.gif')

frame-0.png

frame-0.png

frame-1.png

frame-1.png

这篇关于从GIF提取帧时出现更多问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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