2017-08-07 50 views
0

是否有可能使用scipy misc從web url中讀取圖像,或者我是否有義務將圖像保存在網頁中,然後將其讀取並刪除?我找不到一個優雅的解決方案。scipy misc從網址urlread

from scipy.misc import imread 

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

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

回答

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