2017-05-11 36 views
2

我使用下面的代码:基于如何从图像url(jpeg)读取字节并在Python 2.7中对base64进行编码?

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编码,它发布到谷歌云愿景API。怎么做?

或者Google云视觉API是否可以与图像网址一起使用?

回答

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(output) 
print data 
相关问题