Python resize image and send to google vision function

会有一股神秘感。 提交于 2019-12-06 08:25:15

You can resize the image via PIL/Pillow and then pass it to the client:

replace

with io.open(path, 'rb') as image_file:
    content = image_file.read()

with

# 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()

Code for python3:

Credits : @kristaps

import io
from PIL import Image
img = Image.open(path)
img.thumbnail((512, 512))
buffer = io.BytesIO()
img.save(buffer, "JPEG")
content = buffer.getvalue()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!