From 688f669181a95766a2992ca5b18d3928466844b6 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Fri, 19 Sep 2014 19:25:42 -0500 Subject: [PATCH] Perform im.getdata check up front --- skimage/io/_plugins/pil_plugin.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/skimage/io/_plugins/pil_plugin.py b/skimage/io/_plugins/pil_plugin.py index ac86e645..218a9903 100644 --- a/skimage/io/_plugins/pil_plugin.py +++ b/skimage/io/_plugins/pil_plugin.py @@ -30,10 +30,13 @@ def imread(fname, dtype=None): """ im = Image.open(fname) try: - return pil_to_ndarray(im, dtype) + # this will raise an IOError if the file is not readable + im.getdata()[0] except IOError: raise ValueError('Could not load "%s": make sure you have library ' 'support for "%s" files' % (fname, im.format)) + else: + return pil_to_ndarray(im, dtype) def pil_to_ndarray(im, dtype=None): @@ -59,8 +62,6 @@ def pil_to_ndarray(im, dtype=None): im.shape = shape[::-1] elif 'A' in im.mode: im = im.convert('RGBA') - # this will raise an IOError if the file is not readable - im.getdata()[0] im = np.array(im, dtype=dtype) if fp is not None: fp.close()