Python:将GIF帧转换为PNG [英] Python: Converting GIF frames to PNG
本文介绍了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屋!
查看全文