如何解决如何在python中从Webscraped URL打印图像
我正在构建图像网络抓取程序,并且试图在屏幕上显示图像。我已经看到了将图像保存到文件的方法,但是,我想知道是否存在一种无需将图像保存到文件的方法。
这是我到目前为止的代码:
def url_to_image(url):
# download the image,convert it to a NumPy array,and then read
# it into OpenCV format
resp = requests.get(url)
image = np.asarray(bytearray(res.json()['data'][0]['card_images'][0]['image_url'].encode()))
image = cv2.imdecode(image,cv2.IMREAD_COLOR)
# return the image
return image
print(url_to_image("https://db.ygoprodeck.com/api_internal/v7/cardinfo.php?&num=5000&offset=0&view=List&misc=yes"))
解决方法
问题是您从url
获得了['image_url']
,但是您将其视为图片。
您必须再次使用requests.get()
中的url
和['image_url']
来获取图像。
import requests
import numpy as np
import cv2
def url_to_image(url):
response = requests.get(url)
data = response.json()
image_url = data['data'][0]['card_images'][0]['image_url']
print('image_url:',image_url)
response = requests.get(image_url)
data = np.asarray(bytearray(response.content),dtype="uint8")
image = cv2.imdecode(data,cv2.IMREAD_COLOR)
cv2.imshow('image',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
return image
print(url_to_image("https://db.ygoprodeck.com/api_internal/v7/cardinfo.php?&num=5000&offset=0&view=List&misc=yes"))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。