mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-04 17:23:16 +08:00
Merge pull request #368 from tonysyu/imread-url
ENH: Allow URLs in imread.
This commit is contained in:
+21
-2
@@ -1,6 +1,10 @@
|
||||
__all__ = ['Image', 'imread', 'imread_collection', 'imsave', 'imshow', 'show',
|
||||
'push', 'pop']
|
||||
|
||||
import os
|
||||
import re
|
||||
import urllib2
|
||||
import tempfile
|
||||
from io import BytesIO
|
||||
|
||||
import numpy as np
|
||||
@@ -12,6 +16,14 @@ from skimage.color import rgb2grey
|
||||
# Shared image queue
|
||||
_image_stack = []
|
||||
|
||||
URL_REGEX = re.compile(r'http://|https://|ftp://|file://|file:\\')
|
||||
|
||||
|
||||
def is_url(filename):
|
||||
"""Return True if string is an http or ftp path."""
|
||||
return (isinstance(filename, basestring) and
|
||||
URL_REGEX.match(filename) is not None)
|
||||
|
||||
|
||||
class Image(np.ndarray):
|
||||
"""Class representing Image data.
|
||||
@@ -88,7 +100,7 @@ def imread(fname, as_grey=False, plugin=None, flatten=None,
|
||||
Parameters
|
||||
----------
|
||||
fname : string
|
||||
Image file name, e.g. ``test.jpg``.
|
||||
Image file name, e.g. ``test.jpg`` or URL.
|
||||
as_grey : bool
|
||||
If True, convert color images to grey-scale (32-bit floats).
|
||||
Images that are already in grey-scale format are not converted.
|
||||
@@ -117,7 +129,14 @@ def imread(fname, as_grey=False, plugin=None, flatten=None,
|
||||
if flatten is not None:
|
||||
as_grey = flatten
|
||||
|
||||
img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
|
||||
if is_url(fname):
|
||||
with tempfile.NamedTemporaryFile(delete=False) as f:
|
||||
u = urllib2.urlopen(fname)
|
||||
f.write(u.read())
|
||||
img = call_plugin('imread', f.name, plugin=plugin, **plugin_args)
|
||||
os.remove(f.name)
|
||||
else:
|
||||
img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
|
||||
|
||||
if as_grey and getattr(img, 'ndim', 0) >= 3:
|
||||
img = rgb2grey(img)
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import os
|
||||
|
||||
from numpy.testing import *
|
||||
import numpy as np
|
||||
|
||||
import skimage.io as io
|
||||
from skimage import data_dir
|
||||
|
||||
|
||||
def test_stack_basic():
|
||||
@@ -15,5 +18,15 @@ def test_stack_basic():
|
||||
def test_stack_non_array():
|
||||
io.push([[1, 2, 3]])
|
||||
|
||||
|
||||
def test_imread_url():
|
||||
# tweak data path so that file URI works on both unix and windows.
|
||||
data_path = data_dir.lstrip(os.path.sep)
|
||||
data_path = data_path.replace(os.path.sep, '/')
|
||||
image_url = 'file:///{0}/camera.png'.format(data_path)
|
||||
image = io.imread(image_url)
|
||||
assert image.shape == (512, 512)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_module_suite()
|
||||
|
||||
Reference in New Issue
Block a user