Python:将 GIF 帧转换为 PNG [英] Python: Converting GIF frames to PNG

查看:45
本文介绍了Python:将 GIF 帧转换为 PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm very new to python, trying to use it to split the frames of a GIF into PNG images.

# 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

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

Is this specific to the image I'm working with or am I doing something wrong?

谢谢!

推荐答案

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

I don't think you're doing anything wrong. See a similar issue here: animated GIF problem. It appears as if the palette information isn't correctly treated for later frames. The following works for me:

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)

这篇关于Python:将 GIF 帧转换为 PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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