如何使用来自python的请求从带有opencv的url打开图像 [英] How to open an image from an url with opencv using requests from python
问题描述
我正在尝试在 python 上使用 OpenCV 打开大量图像,因为我需要在后面使用它们.
I am trying to open a large list of images using OpenCV on python, because I need to work with them latter.
其实我可以用这样的枕头来实现这个目标:
Actually, I can achieve this goal with pillow like this:
url = r'https://i.imgur.com/DrjBucJ.png'
response = requests.get(url, stream=True).raw
guess = Image.open(response).resize(size)
我正在使用来自 python 的库 requests
.
I am using the library requests
from python.
response
看起来像这样:b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01\xdb\...
如果我没记错的话,这些是来自 url 图像的像素值,对吗?
And if I am not wrong, those are the values of the pixels from the image of the url, correct?
我的问题是:我怎样才能用 OpenCV 做同样的事情?
My question is: how can I do the same with OpenCV?
我试过了:
resp = requests.get(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
我收到此错误:
image = np.asarray(bytearray(resp.read()), dtype="uint8")
AttributeError: 'Response' object has no attribute 'read'
我从这个网站得到了代码:https://www.pyimagesearch.com/2015/03/02/convert-url-to-image-with-python-and-opencv/
I got the code from this web: https://www.pyimagesearch.com/2015/03/02/convert-url-to-image-with-python-and-opencv/
推荐答案
你刚刚忘记了 requests.get
stream=True 和 .raw
>
You just forgot stream=True
and .raw
in requests.get
resp = requests.get(url, stream=True
).raw
resp = requests.get(url,
stream=True
).raw
import cv2
import numpy as np
import requests
url = r'https://i.imgur.com/DrjBucJ.png'
resp = requests.get(url, stream=True).raw
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
# for testing
cv2.imshow('image',image)
cv2.waitKey(0)
cv2.destroyAllWindows()
<小时>
回答你的问题
To answer your question
.raw
表示您希望以字节流的形式检索响应,而响应不会通过任何措施评估或转换(因此它不会解码 gzip 和 deflate 传输编码),但是使用 .content
gzip 和 deflate 传输-编码会自动为您解码.
.raw
mean that you want to retrieve the response as a stream of bytes and the response will not evaluated or transformed by any measure (so it will not decode gzip and deflate transfer-encodings) but with .content
The gzip and deflate transfer-encodings are automatically decoded for you.
在您的情况下,最好使用 .content
而不是 .raw
In your case it will be better to use .content
over .raw
请求包文档中的以下说明
注意关于使用 Response.iter_content 与 Response.raw 的重要说明.Response.iter_content 将自动解码 gzip 和 deflate 传输编码.Response.raw 是一个原始字节流——它不会转换响应内容.如果您确实需要访问返回的字节,请使用 Response.raw.
Note An important note about using Response.iter_content versus Response.raw. Response.iter_content will automatically decode the gzip and deflate transfer-encodings. Response.raw is a raw stream of bytes – it does not transform the response content. If you really need access to the bytes as they were returned, use Response.raw.
参考:
https://2.python-requests.org/en/master/user/quickstart/#raw-response-content
https://2.python-requests.org/en/master/user/quickstart/#binary-response-content
这篇关于如何使用来自python的请求从带有opencv的url打开图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!