0

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

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

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

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

4

1 回答 1

2

我相信 Quantize 图像方法计划在未来发布可能值得在github上查看计划的更新分支。如果您迫不及待,并且对开发构建感到不自在,您可以通过wand 的 APIctypes直接访问ImageMagick 的 C 库

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
于 2014-04-03T14:20:18.600 回答