image - 如何从 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()
解决方案
使用wand.image.Image.make_blob
方法。
content = blank.make_blob('JPEG')
推荐阅读
- c - 头文件中的结构在源文件中产生错误
- javascript - 在 node.js 中模拟 sinon 注入服务
- image - Symfony 3 中的 ContextErrorException
- python - 将元组列表转换为字典,给每个元组一个不同的键
- php - SQL - 从多个表为不同角色创建视图
- deep-learning - 计算机视觉检测小物体效果不好的原因是什么?
- azure - vsts 构建对特定团队的代理访问权限?
- powershell - 无法将值“Year”转换为类型“System.Int32”。错误:“输入字符串的格式不正确。”
- vba - 在excel中创建数据记录
- xml - xsd 中的限制如何作用于复杂类型?