0

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...

  • Nice first question JunZhang. Could you provide the output from ImageMagick and your python script respectively. It makes it easier for anyone trying to help you to understand your problem. – Aerows Oct 15 '20 at 14:42

1 Answers1

1

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')
emcconville
  • 23,800
  • 4
  • 50
  • 66