2017-05-11 40 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 
相關問題