使用wand进行量化
我想用Python的Wand库从一张图片中获取
convert image.jpg -colors $n -format %c histogram:info:-
Wand的Image
对象有一个直方图字典,可以从中获取颜色列表。但是我找不到一个命令来减少颜色的数量。
Wand能进行颜色简化吗?有没有和-colors
相关的绑定?
1 个回答
2
我相信将来会有一个量化图像的方法,这个方法已经在计划中,可以在这里查看。你也可以去github看看未来的更新分支。如果你等不及了,而且对开发版本不太熟悉,你可以直接通过ImageMagick的C库来使用这个功能,具体可以参考wand的API和ctypes的文档。
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