目前,wand不支持任何的統計方法從ImageMagick的C-API(外histogram和EXIF)。幸運的是wand.api提供擴展功能。
- 在MagickWand的文檔中找到。
- 使用ctypes實現數據類型/結構()
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)
- 擴展
wand.image.Image
,並使用新支持的方法。
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)
感謝您的答案!我已經實現了類似的東西:) – 2014-10-16 20:58:57