使用魔杖量化

时间:2014-04-02 14:51:01

标签: python imagemagick wand

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

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

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

魔杖可以进行色彩还原吗? -colors是否有约束力?

1 个答案:

答案 0 :(得分:2)

我相信量化图像方法是planned for future release。可能值得查看github上的计划更新分支。如果您不能等待,并且不习惯开发构建,则可以直接通过ImageMagick's C library&amp ;;访问wand's APIctypes

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
相关问题