Python调整图像大小并发送到谷歌视觉功能 [英] Python resize image and send to google vision function
本文介绍了Python调整图像大小并发送到谷歌视觉功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于Google视觉对输入图像的大小有一些限制,首先调整输入图像的大小,然后使用 detect_labels()
函数。
Since google vision has some restrictions on input image size, I want to first resize input image and then use the detect_labels()
function.
这是他们的示例代码
def detect_labels(path):
"""Detects labels in the file."""
vision_client = vision.Client()
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = vision_client.image(content=content)
labels = image.detect_labels()
print('Labels:')
for label in labels:
print(label.description)
他们使用 io
打开图像文件。我想知道以这种方式,如何在内存中调整图像大小,然后调用 detect_labels()
吗?
they use io
to open the image file. I wonder in this way, how to resize the image in memory and then call detect_labels()
?
推荐答案
您可以通过PIL / Pillow调整图像的大小,然后将其传递给客户端:
You can resize the image via PIL/Pillow and then pass it to the client:
替换
with io.open(path, 'rb') as image_file:
content = image_file.read()
有
# Warning: untested code, may require some fiddling
import Image, StringIO
img = Image.open(path)
img.thumbnail((512, 512))
buffer = StringIO.StringIO()
img.save(buffer, "PNG")
content = buffer.getvalue()
这篇关于Python调整图像大小并发送到谷歌视觉功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文