相关疑难解决方法(0)

PIL - 将GIF帧转换为JPG

我尝试使用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: …
Run Code Online (Sandbox Code Playgroud)

python jpeg image-processing gif python-imaging-library

16
推荐指数
1
解决办法
2万
查看次数