2014-10-16 43 views
0

我想为图像提取统计信息,如“均值”,“标准差”等。 但是,我无法找到任何与python-关于它的魔杖文档。Python-wand:我如何读取图像属性/统计信息

在命令行中我能得到这样的统计是这样的:

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

convert MyImage.jpg -verbose info: 

如何从使用魔杖Python程序等信息?

回答

2

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

  1. 在MagickWand的文档中找到​​。
  2. 使用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) 
    
    +0

    感谢您的答案!我已经实现了类似的东西:) – 2014-10-16 20:58:57

    1

    只是说明任何人继@emcconville了很好的建议:

    1. 在ImageMagick的网站上的文档是V7.x中
    2. 棒将只与ImageMagick的6个工作。 x
    3. 在IM6.x中,实际上_ChannelStatistics的末尾有一个字段,一个叫做熵的double,如果您将它从ChannelStatistics声明中删除,那么您的结构将无法与wha正确对齐你回来,它会包含一堆废话