【问题标题】:How to convert wand image object to open cv image (numpy array)如何将魔杖图像对象转换为打开 cv 图像(numpy 数组)
【发布时间】:2023-03-12 08:52:02
【问题描述】:

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

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

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

【问题讨论】:

    标签: python-2.7 opencv imagemagick wand


    【解决方案1】:

    您只需写入字节数组缓冲区,然后传递给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)
    

    【讨论】:

    • 只是对它如何工作的评论,IIUC make_blob() 返回以另一种格式编码的图像,显​​然是 BMP,OpenCV 然后可以理解和解码。找到一种直接创建数组的方法会很好。显然 numpy.asarray(img) 应该在这里工作,但它并没有真正做正确的事情,它创建了一个“wand.color.Color”对象数组......
    • 我在处理 pdf 时遇到了 alpha 通道问题。设置背景色后最好放img.alpha_channel = 'remove'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 2010-09-27
    • 2011-12-07
    • 2021-08-08
    相关资源
    最近更新 更多