如何将魔杖图像对象转换为numpy数组(没有OpenCV)?

时间:2017-12-01 18:00:50

标签: python numpy pdf wand ndimage

我正在使用Wand将pdf文件转换为图像。然后,我使用ndimage进行进一步的图像处理。

我想直接将Wand图像转换为ndarray ...我已经看到了答案here,但它使用的是OpenCV。如果不使用OpenCV,这可能吗?

目前我保存了一个临时文件,该文件是用scipy.misc.imread()重新打开的。

4 个答案:

答案 0 :(得分:3)

Wand 0.5.3开始,直接支持

import numpy as np
from wand.image import Image

with Image(filename='rose:') as img:
    array = np.array(img)
    print(array.shape)  #=> (70, 46, 3)

答案 1 :(得分:0)

您可以使用缓冲区,如下所示:

import cStringIO
import skimage.io
from wand.image import Image
import numpy

#create the image, then place it in a buffer
with Image(width = 500, height = 100) as image:
    image.format        = 'bmp'
    image.alpha_channel = False
    img_buffer=numpy.asarray(bytearray(image.make_blob()), dtype=numpy.uint8)

#load the buffer into an array
img_stringIO = cStringIO.StringIO(img_buffer)
img = skimage.io.imread(img_stringIO)

img.shape

答案 2 :(得分:0)

以下是{3}}

的Python3版本
from io import BytesIO
import skimage.io
from wand.image import Image
import numpy

with Image(width=100, height=100) as image:
    image.format = 'bmp'
    image.alpha_channel = False
    img_buffer = numpy.asarray(bytearray(image.make_blob()), dtype='uint8')
bytesio = BytesIO(img_buffer)
img = skimage.io.imread(bytesio)

print(img.shape)

答案 3 :(得分:0)

对于没有缓冲区的Python3,这对我有用:

from wand.image import Image
import numpy
with Image(width=100, height=100) as image:
    image.format = 'gray' #If rgb image, change this to 'rgb' to get raw values
    image.alpha_channel = False
    img_array = numpy.asarray(bytearray(image.make_blob()), dtype='uint8').reshape(image.size)