如何使用魔杖降低gif质量?

时间:2016-12-16 09:13:53

标签: python imagemagick wand

我有以下代码

from wand.image import Image

def saveSizes(f, filename):
    scaled_width = 400
    scaled_hight = 400
    with Image() as finalImage:
        with Image(filename=f) as img:
            for frame in img.sequence:
                #frame.transform(resize="%dx%d" % (scaled_width, scaled_hight))
                frame.compression_quality = 75
                finalImage.sequence.append(frame)
        filename += '.gif'
        finalImage.save(filename = filename)
saveSizes('source_file.gif', 'dest_file')

但是' source_file.gif'的大小。与' dest_file.gif'相同。为什么" compression_quality"属性不起作用?

有没有更好的方法来减少使用wand或其他python库的gif的大小。?

另外,我在控制台中为gif中的每一帧获取以下日志。

Exception ignored in: <bound method Resource.__del__ of <wand.sequence.SingleImage: 901eb12 (200x150)>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/wand/resource.py", line 232, in __del__
    self.destroy()
  File "/usr/local/lib/python3.5/site-packages/wand/sequence.py", line 331, in destroy
    self.container.sequence[self.index] = self
  File "/usr/local/lib/python3.5/site-packages/wand/sequence.py", line 304, in index
    assert image
AssertionError:

1 个答案:

答案 0 :(得分:0)

compression_quality可以正常使用源代码(整个文件)。

我的pdfs工作示例:

def ConvertFewPagePdfToPngs(pdf):
    with wand.image.Image(filename = pdf, resolution = 200) as source:

        source.compression_quality = 99
        imagess = source.sequence
        for i in range(len(imagess)):
            imagess[i].format = 'png'
            destFileName = r'path' # depends on i
            wand.image.Image(imagess[i]).save(filename=destFileName)

当我尝试将compression_quality应用于一个页面时,我显示了与您显示相同的错误