如何将wand图像对象转换为打开cv图像(numpy数组) [英] How to convert wand image object to open cv image (numpy array)

查看:615
本文介绍了如何将wand图像对象转换为打开cv图像(numpy数组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have imported wand using the following code

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)图像对象?

How can i convert img object to open cv (cv2) image object in python?

推荐答案

你只需要写一个字节数组缓冲区,并传递到 cv2.imdecode

You would simply write to a byte-array buffer, and pass to 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)

这篇关于如何将wand图像对象转换为打开cv图像(numpy数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆