簡體   English   中英

如何將魔杖圖像對象轉換為numpy數組(沒有OpenCV)?

[英]How to convert a wand image object to numpy array (without OpenCV)?

我正在使用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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM