scipy misc imread from web url

时间:2017-08-07 09:50:50

标签: python scipy imread

是否有可能使用scipy misc从web url中读取图像,或者我是否有义务从web保存图像然后imread并删除它?我找不到一个优雅的解决方案。

from scipy.misc import imread

url = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRtUPjSSDzevfy47QqVpd8v2Nrt49QNIXeAfE9ntEwYKJtnDZabwg'

img = imread(url, mode='RGB')

1 个答案:

答案 0 :(得分:2)

imread采用文件名或文件对象。您可以使用urllib.request.urlopen(Python 3)打开类似文件的网址,并将文件对象传递给imread

from urllib.request import urlopen
from scipy.misc import imread

url = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRtUPjSSDzevfy47QqVpd8v2Nrt49QNIXeAfE9ntEwYKJtnDZabwg'

with urlopen(url) as file:
    img = imread(file, mode='RGB')

对于Python 2:urllib2.urlopen