PIL - 将GIF帧转换为JPG

Sch*_*ken 16 python jpeg image-processing gif python-imaging-library

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

输入gif是:

来源图片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
Run Code Online (Sandbox Code Playgroud)

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

示例#1 http://longcat.de/gif_example1.jpg

我的第二次尝试是,首先在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
Run Code Online (Sandbox Code Playgroud)

这导致像这样的输出:

示例#2 http://longcat.de/gif_example2.jpg

第一次尝试的优点是,第一帧看起来很不错但是正如你所看到的,其余的都被打破了

我接下来应该尝试什么?

编辑:

我想我更接近解决方案了

示例#3 http://longcat.de/gif_example3.png

我必须使用第一个图像的调色板作为其他图像,并将其与前一帧合并(对于使用diff-images的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
Run Code Online (Sandbox Code Playgroud)

但我实际上不知道,为什么我的透明度是黑色,而不是白色即使我修改调色板(将透明度通道更改为白色)或使用透明度蒙版,背景仍然是黑色

fra*_*xel 19

首先,JPEG不支持透明度!但是,这并不是唯一的问题.至于你移动到下一帧GIFpalette信息丢失(问题WITN PIL? )-所以PIL无法正确地转换为RGBA框架(因此,第一帧是okish,但所有的人都扭曲).所以,解决办法是添加的palette每一帧在后面,(这是你在你的最后的代码示例做,但你的问题在于你保存为RGBRGBA那么你有没有Alpha /透明通道.另外你做一些不必要的事情..).无论如何,这里是.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')
Run Code Online (Sandbox Code Playgroud)