如何将wand图像对象转换为打开cv图像(numpy数组)

时间:2016-05-03 23:21:05

标签: python-2.7 opencv imagemagick wand

我使用以下代码导入了魔杖

from wand.image import Image as WandImage
from wand.color import Color
with WandImage(filename=source_file, resolution=(RESOLUTION,RESOLUTION)) as img:
    img.background_color = Color('white')
    img.format        = 'tif'
    img.alpha_channel = False

如何将 img 对象转换为在python中打开cv(cv2)图像对象?

1 个答案:

答案 0 :(得分:6)

您只需写入字节数组缓冲区,然后传递给cv2.imdecode

from wand.image import Image as WandImage
from wand.color import Color
import numpy
import cv2

RESOLUTION=72
source_file='rose:'
img_buffer=None

with WandImage(filename=source_file, resolution=(RESOLUTION,RESOLUTION)) as img:
    img.background_color = Color('white')
    img.format        = 'tif'
    img.alpha_channel = False
    # Fill image buffer with numpy array from blob
    img_buffer=numpy.asarray(bytearray(img.make_blob()), dtype=numpy.uint8)

if img_buffer is not None:
    retval = cv2.imdecode(img_buffer, cv2.IMREAD_UNCHANGED)