0

我正在尝试提取图像的统计信息,例如“平均值”、“标准偏差”等。但是,我在 python-wand 文档中找不到任何相关的信息。

从命令行我可以得到这样的统计数据:

convert MyImage.jpg -format '%[standard-deviation], %[mean], %[max], %[min]' info:

或者

convert MyImage.jpg -verbose info:

如何使用 wand 从 python 程序中获取此类信息?

4

2 回答 2

2

目前,不支持 ImageMagick 的 C-API 中的任何统计方法(在histogramEXIF之外)。幸运的是,提供了wand.api用于扩展功能。

  1. 在 MagickWand 的文档中找到您需要的方法。
  2. 使用ctypes实现数据类型/结构(参考头.h文件
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)
  1. 扩展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-16T17:48:21.557 回答
1

请注意@emcconville 的优秀建议的任何人:

  1. imagemagick 网站上的文档适用于 v7.x
  2. Wand 仅适用于 imagemagick 6.x
  3. 在 IM6.x 中,_ChannelStatistics 的末尾实际上还有一个字段,称为熵,如果您将其排除在 ChannelStatistics 声明之外,您的结构将无法与您返回的内容正确对齐,它将包含一个一堆废话
于 2017-03-01T18:05:40.493 回答