gpt4 book ai didi

python - PIL - 将 GIF 帧转换为 JPG

转载 作者:太空狗 更新时间:2023-10-29 16:54:39 28 4
gpt4 key购买 nike

我尝试使用 Python 图像库将 gif 转换为单个图像,但它会导致奇怪的帧

输入的 gif 是:

Source Image http://longcat.de/gif_example.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

但它会产生奇怪的输出文件:

Example #1 http://longcat.de/gif_example1.jpg

我的第二次尝试是先将 gif 转换为 RGBA,然后使用它的透明蒙版,使透明部分变白:

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

结果如下:

Example #2 http://longcat.de/gif_example2.jpg

与第一次尝试相比的优势在于,第一帧看起来相当不错但是如你所见,其余的都坏了

接下来我应该尝试什么?

编辑:

我想我离解决方案更近了很多

Example #3 http://longcat.de/gif_example3.png

我不得不为其他图像使用第一张图像的调色板,并将其与前一帧合并(对于使用的 gif 动画差异图像)

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

但我其实不知道,为什么我的透明度是黑色的,而不是白色的即使我修改调色板(将透明 channel 更改为白色)或者使用透明 mask ,背景还是黑色

最佳答案

首先,JPEG 不支持透明度!但这不是唯一的问题。当您移动到 ​​GIF 的下一帧时,palette 信息会丢失 (problem witn PIL?) - 所以 PIL 无法正确转换为 RGBA 框架(因此第一帧还可以,但其他所有的都乱七八糟)。所以解决方法是为每一帧重新添加 palette,(这是你在上一个代码示例中所做的,但你的问题是你保存为 RGB 而不是 RGBA 所以你没有 alpha/透明 channel 。而且你做了一些不必要的事情..)。无论如何,这里是具有透明度的 .png 和更正后的代码,希望它有一些用处:)

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

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')

关于python - PIL - 将 GIF 帧转换为 JPG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10269099/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com