我正在使用Wand将 pdf 文件转换为图像。然后,我使用 ndimage 进行进一步的图像处理。
我想直接将 Wand 图像转换为 ndarray ...我在这里看到了答案,但它使用 OpenCV。在不使用 OpenCV 的情况下这可能吗?
目前我保存了一个临时文件,该文件用 scipy.misc.imread() 重新打开
从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)
您可以使用缓冲区,如下所示:
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
这是MrMartin答案的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)
以下是没有缓冲区的 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)