【问题标题】:Python-wand: How can I read image properties/statisticsPython-wand:如何读取图像属性/统计信息
【发布时间】:2014-10-16 12:32:46
【问题描述】:

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

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

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

convert MyImage.jpg -verbose info:

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

【问题讨论】:

    标签: python image-processing statistics imagemagick wand


    【解决方案1】:

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

    1. 在 MagickWand 的文档中查找 method you need
    2. 使用ctypes 实现数据类型/结构 (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)
    
    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)
    

    【讨论】:

    • 感谢您的回答!我已经实现了类似的东西:)
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 2019-04-21
      • 1970-01-01
      相关资源
      最近更新 更多