python gif转jpg_python – PIL – 将GIF框架转换为JPG

我尝试使用

Python Image Library将gif转换为单个图像,

但它会导致奇怪的帧

输入gif是:

在我的第一次尝试中,我尝试将Image.new的图像转换为

RGB图像,255,255,255作为白色背景 – 像任何其他

在互联网上找到的例子:

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

但它会导致奇怪的输出文件:

我的第二个尝试是首先转换RGBA中的gif,然后使用

其透明面具,使透明片白色:

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

这导致如下输出:

第一次尝试的优势是,第一帧看起来不错

但是你可以看到,其余的都是坏的

我下一步该怎么办?

编辑:

我想我更接近解决方案

我不得不使用第一个图像的调色板为其他图像,

并将其与前一帧合并(对于使用的gif动画

DIFF图像)

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

但我实际上不知道,为什么我的透明度是黑色而不是白色

即使我修改调色板(将透明度通道更改为白色)

或使用透明蒙版,背景依然是黑色

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值