Currently, wand doesn't support any of the statistic methods from ImageMagick's C-API (outside of histogram and EXIF). Luckily the wand.api is offered for extending functionality.
- Find the method you need in MagickWand's documentation.
- Use ctypes to implement data types/structures (reference header
.h
files)
from wand.api import library
import ctypes
class ChannelStatistics(ctypes.Structure):
_fields_ = [('depth', ctypes.c_size_t),
('minima', ctypes.c_double),
('maxima', ctypes.c_double),
('sum', ctypes.c_double),
('sum_squared', ctypes.c_double),
('sum_cubed', ctypes.c_double),
('sum_fourth_power', ctypes.c_double),
('mean', ctypes.c_double),
('variance', ctypes.c_double),
('standard_deviation', ctypes.c_double),
('kurtosis', ctypes.c_double),
('skewness', ctypes.c_double)]
library.MagickGetImageChannelStatistics.argtypes = [ctypes.c_void_p]
library.MagickGetImageChannelStatistics.restype = ctypes.POINTER(ChannelStatistics)
- Extend
wand.image.Image
, and use the newly supported methods.
from wand.image import Image
class MyStatisticsImage(Image):
def my_statistics(self):
"""Calculate & return tuple of stddev, mean, max, & min."""
s = library.MagickGetImageChannelStatistics(self.wand)
# See enum ChannelType in magick-type.h
CompositeChannels = 0x002F
return (s[CompositeChannels].standard_deviation,
s[CompositeChannels].mean,
s[CompositeChannels].maxima,
s[CompositeChannels].minima)