【问题标题】:How to read bytes from a image url (jpeg) and encode in base64 on Python 2.7? [duplicate]如何从图像 url (jpeg) 读取字节并在 Python 2.7 上以 base64 编码? [复制]
【发布时间】:2017-05-11 22:27:17
【问题描述】:

我正在使用以下代码:

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 是否适用于图片网址?

【问题讨论】:

    标签: python google-cloud-vision


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 2011-06-02
      • 2013-11-10
      • 2018-04-01
      • 2010-10-31
      • 2022-10-19
      • 1970-01-01
      相关资源
      最近更新 更多