2014-10-16 37 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正確對齊你回來,它會包含一堆廢話