从GIF到PNG提取帧时数据丢失? [英] Loss of data when extracting frames from GIF to PNG?

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

问题描述

当我尝试使用fraxel的答案

When I try to use fraxel's answer on

http://stackoverflow.com/questions/10269099/pil-convert-gif-图像 http://24.media.tumblr.com/fffcc2d8e980fbba4f87d51ed4916b87/上的帧到jpg

tumblr_mh8uaqMo2I1rkp3avo2_250.gif

on the image http://24.media.tumblr.com/fffcc2d8e980fbba4f87d51ed4916b87/tumblr_mh8uaqMo2I1rkp3avo2_250.gif

我为某些人获得了正确的数据,但是对于某些人来说,我看起来像是缺少数据,例如

I get ok data for some, but then for some I get missing data it looks like, e.g.



显示这些我使用imagemagick的显示foo * 然后使用空格来移动图像...图像标记是否可能读错了?

To display these I use imagemagick's display foo* and then use space to move through the images ... is it possible imagemagick is reading them wrong?

编辑:

即使使用转换然后通过显示foo * 我也会得到以下

Even when using convert and then displaying via display foo* I get the following




这可能是gif的特征吗?

Could this be a characteristic of the gif then?

推荐答案

如果您坚持使用ImageMagick,那么解决这个问题非常简单:

If you can stick to ImageMagick then it is very simple to solve this:

convert input.gif -coalesce output.png

否则,你将必须考虑如何构建每个GIF框架的不同形式。对于这种特定类型的GIF,以及您在其他问题中显示的另一个,下面的代码可以正常工作(请注意,在您之前的问题中,接受的答案并不能使所有拆分部分透明 - 至少是最新的发布PIL):

Otherwise, you will have to consider the different forms of how each GIF frame can be constructed. For this specific type of GIF, and also the other one shown in your other question, the following code works (note that in your earlier question, the accepted answer doesn't actually make all the split parts transparent -- at least with the latest released PIL):

import sys
from PIL import Image, ImageSequence

img = Image.open(sys.argv[1])

pal = img.getpalette()
prev = img.convert('RGBA')
prev_dispose = True
for i, frame in enumerate(ImageSequence.Iterator(img)):
    dispose = frame.dispose

    if frame.tile:
        x0, y0, x1, y1 = frame.tile[0][1]
        if not frame.palette.dirty:
            frame.putpalette(pal)
        frame = frame.crop((x0, y0, x1, y1))
        bbox = (x0, y0, x1, y1)
    else:
        bbox = None

    if dispose is None:
        prev.paste(frame, bbox, frame.convert('RGBA'))
        prev.save('foo%02d.png' % i)
        prev_dispose = False
    else:
        if prev_dispose:
            prev = Image.new('RGBA', img.size, (0, 0, 0, 0))
        out = prev.copy()
        out.paste(frame, bbox, frame.convert('RGBA'))
        out.save('foo%02d.png' % i)

最终你必须重新创建 -coalesce 的确如此,因为上面的代码可能不适用于某些GIF图像。

Ultimately you will have to recreate what -coalesce does, since it is likely that the code above may not work with certain GIF images.

这篇关于从GIF到PNG提取帧时数据丢失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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