convert ImageMagick command line argument to Wand

152 Views Asked by At

I'm wondering how to convert this working command line sequence for ImageMagick into a Python script using the Wand library:

convert test.gif -fuzz 5% -layers Optimize test5.gif

Python code is:

from wand.api import library
from wand.color import Color
from wand.drawing import Drawing
from wand.image import Image
import ctypes
library.MagickSetImageFuzz.argtypes = (ctypes.c_void_p,
                                       ctypes.c_double)
with Image(filename='test.gif') as img:
    library.MagickSetImageFuzz(img.wand, img.quantum_range * 0.05)
    with Drawing() as ctx:
        ctx(img)
    img.optimize_layers()
    img.save(filename='test5.gif')

But, I got a different result from the ImageMagick command line. Why...

1

There are 1 best solutions below

0
On

This matches the CLI, but results may vary if the gif is animated or previously optimized.

from wand.image import Image

with Image(filename='test.gif') as img:
    img.fuzz = img.quantum_range * 0.05
    img.optimize_layers()
    img.save(filename='test5.gif')