How to reduce gif quality using wand?

483 views Asked by At

I have the following code

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

But the size of 'source_file.gif' is same as that of 'dest_file.gif'. Why is the "compression_quality" attribute not working?

Is there a better way to reduce the size of gif using wand or some other python lib.?

Also I am getting the following log in the console for every frame in the 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

There are 1 answers

0
Vova On

compression_quality works fine with the source (whole file).

my working example with 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)

When i tried apply compression_quality to one page i got same error as you show