From 1a27bea7da1018d2154d0ae8b487f1f9cd560095 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sun, 12 Jul 2015 15:49:18 -0500 Subject: [PATCH] Allow pil imread to accept file-like objects again --- skimage/io/_plugins/pil_plugin.py | 34 +++++++++++++++++-------------- skimage/io/tests/test_pil.py | 3 +-- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/skimage/io/_plugins/pil_plugin.py b/skimage/io/_plugins/pil_plugin.py index 379ffbd9..475eb99c 100644 --- a/skimage/io/_plugins/pil_plugin.py +++ b/skimage/io/_plugins/pil_plugin.py @@ -13,8 +13,8 @@ def imread(fname, dtype=None, img_num=None, **kwargs): Parameters ---------- - fname : str - File name. + fname : str or file + File name or file-like-object. dtype : numpy dtype object or string specifier Specifies data type of array elements. img_num : int, optional @@ -43,20 +43,13 @@ def imread(fname, dtype=None, img_num=None, **kwargs): if fname.lower().endswith(('.tiff', '.tif')): return tif_imread(fname, **kwargs) - with open(fname, 'rb') as f: - im = Image.open(f) - try: - # this will raise an IOError if the file is not readable - im.getdata()[0] - except IOError as e: - site = "http://pillow.readthedocs.org/en/latest/installation.html#external-libraries" - pillow_error_message = str(e) - error_message = ('Could not load "%s" \n' - 'Reason: "%s"\n' - 'Please see documentation at: %s') % (fname, pillow_error_message, site) - raise ValueError(error_message) - else: + if isinstance(fname, string_types): + with open(fname, 'rb') as f: + im = Image.open(f) return pil_to_ndarray(im, dtype=dtype, img_num=img_num) + else: + im = Image.open(f) + return pil_to_ndarray(im, dtype=dtype, img_num=img_num) def pil_to_ndarray(im, dtype=None, img_num=None): @@ -67,6 +60,17 @@ def pil_to_ndarray(im, dtype=None, img_num=None): Refer to ``imread``. """ + try: + # this will raise an IOError if the file is not readable + im.getdata()[0] + except IOError as e: + site = "http://pillow.readthedocs.org/en/latest/installation.html#external-libraries" + pillow_error_message = str(e) + error_message = ('Could not load "%s" \n' + 'Reason: "%s"\n' + 'Please see documentation at: %s' + % (im.filename, pillow_error_message, site)) + raise ValueError(error_message) frames = [] grayscale = None i = 0 diff --git a/skimage/io/tests/test_pil.py b/skimage/io/tests/test_pil.py index 1a7b7ff1..935dc6a7 100644 --- a/skimage/io/tests/test_pil.py +++ b/skimage/io/tests/test_pil.py @@ -166,8 +166,7 @@ def test_imsave_filelike(): # read from file-like object s.seek(0) - im = Image.open(s) - out = pil_to_ndarray(im) + out = imread(s) assert out.shape == shape assert_allclose(out, image)