从网页链接直接将图像加载到NumPy数组(Python) [英] Loading Image from webpage link directly into NumPy Array (Python)

查看:203
本文介绍了从网页链接直接将图像加载到NumPy数组(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Web上的JPEG图像资源转换为NumPy阵列图像表示形式,该表示形式类似于

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.

这篇关于从网页链接直接将图像加载到NumPy数组(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆