[英]How to convert a wand image object to numpy array (without OpenCV)?
從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.