首页 > 解决方案 > 如何从 wand.image.Image 读取图像而不将其保存到驱动器

问题描述

我应该在此代码中进行哪些更改,这样我就不必在步骤 [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()

标签: imageimage-processingcomputer-visionimagemagickpython-imaging-library

解决方案


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

content = blank.make_blob('JPEG')

推荐阅读