python工具将gif转成图片,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?

Thanks!

解决方案

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值