1

How can I access an unsupported Wand API via the Python Wand interface? For example, I wish to call the Wand API MagickAddNoiseImage but it is not available in the Python interface.

edA-qa mort-ora-y
  • 30,295
  • 39
  • 137
  • 267

1 Answers1

1

Accessing unsupported APIs is pretty easy with wand.api, but you will need to open-up ImageMagick's docs/header files for reference.

from wand.api import library
import ctypes

# Re-create NoiseType enum
NOISE_TYPES = ('undefined', 'uniform', 'gaussian', 'multiplicative_gaussian',
               'impulse', 'laplacian', 'poisson', 'random')
# Map API i/o
library.MagickAddNoiseImage.argtypes = [ctypes.c_void_p,
                                        ctypes.c_uint]
library.MagickAddNoiseImage.restype = ctypes.c_int

# Extend wand's Image class with your new API
from wand.image import Image
class MySupportedImage(Image):
  def add_noise(self, noise_type):
    """My MagickAddNoiseImage"""
    if noise_type not in NOISE_TYPES:
      self.raise_exception()
    return library.MagickAddNoiseImage.argtypes(self.resource,
                                                NOISE_TYPES.index(noise_type))

If your solution works out, think about submitting your solution back to the community (after you've created a solid unit test.)

emcconville
  • 23,800
  • 4
  • 50
  • 66