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

How to read bytes from a image url (jpeg) and encode in base64 on Python 2.7?

我正在使用以下代码:

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 是否与图像 url 一起使用?

假设不需要解析图像,只需抓取并编码即可:

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