如何从图像url(jpeg)读取字节并在Python 2.7上的base64中编码?

时间:2017-05-11 22:27:18

标签: python google-cloud-vision

我使用以下代码:

import urllib, cStringIO
from PIL import Image  
url='https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png'
file = cStringIO.StringIO(urllib.urlopen(url).read())
img = Image.open(file)

基于:How do I read image data from a URL in Python?

现在,我需要对其进行base64编码,以便发布到Google Cloud Vision API。怎么做?

或者Google Cloud Vision API是否与图片网址一起使用?

1 个答案:

答案 0 :(得分:1)

假设没有必要解析图像,只需抓住它并对其进行编码:

import urllib2
import base64
url = 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png'
contents = urllib2.urlopen(url).read()
data = base64.b64encode(contents)
print data