2016-05-03 9 views

答えて

5

単純にバイト配列バッファに書き込み、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) 
+0

ありがとうございました。 –

+2

これはどのように動作するのか、IIUC make_blob()は、別の形式でエンコードされた画像を返します.BMPは明らかにOpenCVが理解してデコードすることができます。配列を直接作成する方法を見つけることは良いことです。明らかにnumpy.asarray(img)はここで動作するはずですが、実際には正しいことはしません。 "wand.color.Color"オブジェクトの配列を作成します。 – dividebyzero

+0

アルファチャンネルの問題に直面しました。 pdf。背景色を設定した後に 'img.alpha_channel = 'remove''を置く方が良いでしょう。 –

関連する問題