目前,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