I'm trying to get a JPEG image resource from the web into a NumPy array image representation similar to the array returned by scipy.misc.imread
. Instead of saving the image to disk, as in the example below:
import requests
from scipy import misc
def load_image(url):
res = requests.get(url)
if res == 200 and 'jpeg' in res.headers['content-type']:
with open('image.jpg', 'wb') as fp:
for chunk in res:
fp.write(chunk)
img_arr = misc.imread('image.jpg')
return img_arr
else:
return None
I'd like to load the image directly into memory. Is there a way to do so?
Since you mentioned scipy.misc.imread
, we could use it to hide the part of Image.open
. Thus, the implementation would look like this -
from scipy import misc
res = requests.get(url)
img_arr = misc.imread(BytesIO(res.content))
Performance-wise it seems comparable to the four stages of conversion as listed in the other post.