【问题标题】:Quantize using wand使用魔杖量化
【发布时间】:2014-04-02 14:51:00
【问题描述】:

我想使用 Python Wand 检索图像中的n 平均颜色列表。在命令行中,这可以直接使用 Imagemagick 来实现。

convert image.jpg -colors $n -format %c histogram:info:-

Wands Image 对象有一个直方图字典,可以从中访问颜色列表。但我找不到量化颜色的命令。

Wand 可以减色吗? -colors 有绑定吗?

【问题讨论】:

    标签: python imagemagick wand


    【解决方案1】:

    我相信量化图像方法是planned for future release。在github 上查看计划中的更新分支可能是值得的。如果您迫不及待,并且对开发版本不满意,您可以通过wand's APIctypes 直接访问ImageMagick's C library

    from wand.image import Image
    from wand.api import library
    import ctypes
    
    # Register C-type arguments
    library.MagickQuantizeImage.argtypes = [ctypes.c_void_p,
                                            ctypes.c_size_t,
                                            ctypes.c_int,
                                            ctypes.c_size_t,
                                            ctypes.c_int,
                                            ctypes.c_int
                                           ]   
    library.MagickQuantizeImage.restype = None
    
    def MyColorRedection(img,color_count):
      '''
         Reduce image color count
      '''
      assert isinstance(img,Image)
      assert isinstance(color_count,int)
      colorspace = 1 # assuming RGB?
      treedepth = 8 
      dither = 1 # True
      merror = 0 # False
      library.MagickQuantizeImage(img.wand,color_count,colorspace,treedepth,dither,merror)
    
    with Image(filename="image.jpg") as img:
      MyColorRedection(img,8) # "img' has now been reduced to 8 colors
    

    【讨论】:

      猜你喜欢
      • 2017-12-24
      • 2013-06-23
      • 2015-08-22
      • 2015-07-07
      • 2017-10-27
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多