【问题标题】:More problems extracting frames from GIFs从 GIF 中提取帧的更多问题
【发布时间】:2014-03-26 15:27:09
【问题描述】:

在我之前的问题 (Gifs opened with python have broken frames) 之后,我现在有了有时可以工作的代码。

比如这段代码

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 时:

它产生这两个帧:

第一个还可以,第二个没那么多。

现在呢?

【问题讨论】:

  • 尝试查看第二帧而不用第一帧进行堆肥,看看它是否是它应该是什么像素已经改变和没有改变。

标签: python gif


【解决方案1】:

听起来你想这样做:

while True:
    try:
        current.save('original%d.gif' % counter)
        img.seek(img.tell()+1)
        current = img.convert('RGBA')
        counter += 1
    except EOFError:
        break

【讨论】:

  • 您并没有真正阅读上一个问题,是吗?我之前的问题正是那个代码不起作用。
  • 太棒了。那么你的问题是什么?这种方法或者另一个SO'er提供的方法是错误的呢?
  • 在我的问题中,我说问题是什么:该代码在应用于某些 gif 时仍然会产生错误的结果。
  • 结果有什么问题。您显示的结果看起来像是在另一个图像之上合成图像。我的解决方案应该(大概)是没有合成的图像。
  • 如果您阅读我之前的问题,您会发现构图是必要的,因为 gif 只保存逐帧的更改。所以你所拥有的不是“解决方案”。请阅读链接的问题。
【解决方案2】:

试试Wand(Wand 是一个基于 ctypes 的 Python 简单 ImageMagick 绑定。)

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-1.png

【讨论】:

    猜你喜欢
    • 2014-12-22
    • 2010-10-31
    • 1970-01-01
    • 2019-01-02
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    相关资源
    最近更新 更多