我正在尝试提取图像的统计信息,例如“平均值”、“标准偏差”等。但是,我在 python-wand 文档中找不到任何相关的信息。
从命令行我可以得到这样的统计数据:
convert MyImage.jpg -format '%[standard-deviation], %[mean], %[max], %[min]' info:
或者
convert MyImage.jpg -verbose info:
如何使用 wand 从 python 程序中获取此类信息?
我正在尝试提取图像的统计信息,例如“平均值”、“标准偏差”等。但是,我在 python-wand 文档中找不到任何相关的信息。
从命令行我可以得到这样的统计数据:
convert MyImage.jpg -format '%[standard-deviation], %[mean], %[max], %[min]' info:
或者
convert MyImage.jpg -verbose info:
如何使用 wand 从 python 程序中获取此类信息?
目前,wand不支持 ImageMagick 的 C-API 中的任何统计方法(在histogram和EXIF之外)。幸运的是,提供了wand.api用于扩展功能。
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)
请注意@emcconville 的优秀建议的任何人: