如何从wand.image.Image读取图像而不将其保存到驱动器

时间:2018-08-23 16:33:38

标签: image image-processing computer-vision imagemagick python-imaging-library

我应该对这段代码进行什么更改,这样我就不必在步骤[A]中将图像保存到磁盘,然后在步骤[B]中再次从磁盘读取图像。如代码所示。有人可以通过代码更改或一些提示来帮助我吗?

import io
import os
import six
from google.cloud import vision 
from google.cloud import translate
from google.cloud.vision import types
import json
from wand.image import Image

client = vision.ImageAnnotatorClient()

sample_pdf = Image(filename='CMB72_CMB0720160.pdf[0]', resolution=500)
blank = Image(filename='Untitled.png')
all_ = sample_pdf.clone()
polling_ = sample_pdf.clone()
voters = sample_pdf.clone()
all_.crop(3000,2800,3800,3860)       
polling_.crop(870,4330,2900,4500)
voters.crop(1300,4980,2000,5250)
blank.composite(all_,left=0,top=0)
blank.composite(voters,left=0,top=1100)
blank.composite(polling_,left=0,top=1420)
blank.save('CMB72_CMB0720122.jpg')---------------[A]


file_name = 'CMB72_CMB0720122.jpg'-------------|
with io.open(file_name,'rb') as image_file:----|>[B]
     content = image_file.read()---------------|

image = types.Image(content= content)

image_context = vision.types.ImageContext(
        language_hints=['hi'])       

response = client.document_text_detection(image=image)
texts = response.text_annotations
file = open('jin.txt','w+',encoding='utf-8')
file.write(texts[0].description)
file.close()

1 个答案:

答案 0 :(得分:0)

使用wand.image.Image.make_blob方法。

content = blank.make_blob('JPEG')