【问题标题】:Python: Converting GIF frames to PNGPython:将 GIF 帧转换为 PNG
【发布时间】:2011-06-21 18:06:42
【问题描述】:

我对 python 很陌生,试图用它来将 GIF 的帧分割成 PNG 图像。

# Using this GIF: http://www.videogamesprites.net/FinalFantasy1/Party/Before/Fighter-Front.gif

from PIL import Image

im = Image.open('Fighter-Front.gif')
transparency = im.info['transparency'] 
im.save('test1.png', transparency=transparency)

im.seek(im.tell()+1)
transparency = im.info['transparency'] 
im.save('test2.png', transparency=transparency)

# First frame comes out perfect, second frame (test2.png) comes out black,
# but in the "right shape", i.e. 
# http://i.stack.imgur.com/5GvzC.png

这是特定于我正在使用的图像还是我做错了什么?

谢谢!

【问题讨论】:

    标签: python png python-imaging-library animated-gif


    【解决方案1】:

    我不认为你做错了什么。在此处查看类似问题:animated GIF problem。看起来好像调色板信息没有为以后的帧正确处理。以下对我有用:

    def iter_frames(im):
        try:
            i= 0
            while 1:
                im.seek(i)
                imframe = im.copy()
                if i == 0: 
                    palette = imframe.getpalette()
                else:
                    imframe.putpalette(palette)
                yield imframe
                i += 1
        except EOFError:
            pass
    
    for i, frame in enumerate(iter_frames(im)):
        frame.save('test%d.png' % i,**frame.info)
    

    【讨论】:

      【解决方案2】:

      我已经在https://code.launchpad.net/~asdfghjkl-deactivatedaccount1/python-imaging/gif-fix 修复了这个错误。

      如果 GIF 使用本地颜色表,则 DSM 的答案将不起作用。

      【讨论】:

      • 确实,在接受的答案中替换调色板是一件非常错误的事情。但是,不需要应用补丁,是否检查frame.palette.dirty 来决定是否放置“初始”调色板只是一个问题吗?
      • 你能为此向 Pillow 提交拉取请求吗? github.com/python-imaging/Pillow/issues/…
      猜你喜欢
      • 2021-02-01
      • 2014-03-17
      • 2012-01-25
      • 1970-01-01
      • 2023-03-23
      • 2012-09-15
      • 2013-06-05
      • 2012-05-03
      • 2011-01-09
      相关资源
      最近更新 更多