python PIL将[animated]gif帧转换为JPG,但有瑕疵

2024-04-20 00:12:48 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:我最初搜索信息的问题是我没有区分gif和“动画gif”。因此有很多资源来处理这个问题。 资源: Link 3

显然,PIL本身在处理动画gif方面能力不足。在

我想把gif帧转换成jpg格式。对于一些gif(主要是黑白的)来说,这很好,但是对于其他的(主要是彩色的)就不那么好了。我看了一些SO上的帖子,试过了,但都没用。我特别尝试了:Link1Link2。在

性能是一个温和的考虑因素,但目前我只想要一个可行的解决方案。一个一致的模式是,gif的第一个图像总是完美的。有趣的是,我甚至尝试过Zamzar,它也产生了相同的噪声数据。我在做一些研究,似乎这可能是LZW压缩算法的问题,尽管如此,我看到一些帖子建议PIL负责LZW解压。另一方面,我听说LZW减压是有缓解作用的。在

请注意,我也尝试过转换成PNG,但也没有成功。白点是在图像的顶部还是什么?在

这里有一个sample gif,它产生了这个错误。在

编辑:我刚碰到images2gif.py。我会更新这篇文章,如果它能解决这个问题。在

下面是我使用的代码:

from PIL import Image
import sys
import os

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("RGB", im.size)

            #new_im = Image.new("RGB", im.size)
            new_im.paste(im)
            new_im.save('foo'+str(i)+'.png')
            #if(os.stat('foo' + str(i)+'.png')):
                # os.remove('foo' + str(i) + '.jpg')
             i += 1
             mypalette = im.getpalette()
             im.seek(im.tell() + 1)

     except EOFError:
         pass # end of sequence

Tags: imageimport编辑newpilfooos动画