在Python中读取和使用从网址请求的图片

我目前正在尝试使用Google的python视觉库。但是我目前仍停留在如何从网络上读取图像上。到目前为止,我已经在下面进行了介绍。我的问题是内容似乎总是空的,当我使用PyCharm检查时,它说它仅包含b''

如何打开该图像以便将其用于Google的图书馆?

from google.cloud import vision
from google.cloud.vision import types
from urllib import request
import io

client = vision.ImageAnnotatorClient.from_service_account_json('cred.json')

url = "https://cdn.getyourguide.com/img/location_img-59-1969619245-148.jpg"

img = request.urlopen(url)
with io.open('location_img-59-1969619245-148.jpg','rb') as fhand:
    content = fhand.read()
image = types.Image(content=content)
response = client.label_detection(image=image)
labels = response.label_annotations

print('Labels:')
for label in labels:
    print(label.description)
ysw9232 回答:在Python中读取和使用从网址请求的图片

您是否尝试通过requests库获取图片?

import requests 

r = requests.get("https://cdn.getyourguide.com/img/location_img-59-1969619245-148.jpg") 
o = open("location_img.jpg","wb")
o.write(r.content)
o.close()
本文链接:https://www.f2er.com/3091807.html

大家都在问