python gif转jpg,PIL - 将GIF帧转换为JPG

I tried to convert an gif to single images with Python Image Library,

but it results in weird frames

The Input gif is:

In my first try, i tried to convert the image with Image.new to an

RGB image, with 255,255,255 as white background - like in any other

example i've found on the internet:

def processImage( infile ):

try:

im = Image.open( infile )

except IOError:

print "Cant load", infile

sys.exit(1)

i = 0

try:

while 1:

background = Image.new("RGB", im.size, (255, 255, 255))

background.paste(im)

background.save('foo'+str(i)+'.jpg', 'JPEG', quality=80)

i += 1

im.seek( im.tell() + 1 )

except EOFError:

pass # end of sequence

but it results in weird output files:

My second try was, to convert the gif in an RGBA first, and then use

its transparency mask, to make the transparent pieces white:

def processImage( infile ):

try:

im = Image.open( infile )

except IOError:

print "Cant load", infile

sys.exit(1)

i = 0

try:

while 1:

im2 = im.convert('RGBA')

im2.load()

background = Image.new("RGB", im2.size, (255, 255, 255))

background.paste(im2, mask = im2.split()[3] )

background.save('foo'+str(i)+'.jpg', 'JPEG', quality=80)

i += 1

im.seek( im.tell() + 1 )

except EOFError:

pass # end of sequence

which results in an output like this:

The advantage over the first try was, that the first frame looks pretty good

But as you can see, the rest is broken

What should i try next?

Edit:

I think i came a lot closer to the solution

I had to use the palette of the first image for the other images,

and merge it with the previous frame (for gif animations which use

diff-images)

def processImage( infile ):

try:

im = Image.open( infile )

except IOError:

print "Cant load", infile

sys.exit(1)

i = 0

size = im.size

lastframe = im.convert('RGBA')

mypalette = im.getpalette()

try:

while 1:

im2 = im.copy()

im2.putpalette( mypalette )

background = Image.new("RGB", size, (255,255,255))

background.paste( lastframe )

background.paste( im2 )

background.save('foo'+str(i)+'.png', 'PNG', quality=80)

lastframe = background

i += 1

im.seek( im.tell() + 1 )

except EOFError:

pass # end of sequence

But i actually dont know, why my transparency is black, instead of white

Even if i modify the palette (change the transparency channel to white)

or use the transparency mask, the background is still black

解决方案

First of all, JPEG doesn't support transparency! But that's not the only problem.. As you move to the next frame of the GIF the palette information is lost (problem witn PIL?) - so PIL is unable to correctly convert to the RGBA framework (Hence the first frame is okish, but all the others are screwy). So the work-around is to add the palette back in for every frame, (which is what you were doing in your last code example, but your trouble was that you were saving as RGB not RGBA so you had no alpha/ transparency channel. Also you were doing a few unnecessary things..). Anyhow, here are the .png's with transparency and the corrected code, hope its of some use :)

KCD9p.png

SAu7p.png

g0Lfo.png

00AAc.png

2c17L.png

nutsv.png

d6n3T.png

V13hc.png

jWywv.png

4dlAK.png

import Image

import sys

def processImage(infile):

try:

im = Image.open(infile)

except IOError:

print "Cant load", infile

sys.exit(1)

i = 0

mypalette = im.getpalette()

try:

while 1:

im.putpalette(mypalette)

new_im = Image.new("RGBA", im.size)

new_im.paste(im)

new_im.save('foo'+str(i)+'.png')

i += 1

im.seek(im.tell() + 1)

except EOFError:

pass # end of sequence

processImage('gif_example.gif')

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值