mirror of
https://github.com/wassname/scikit-image.git
synced 2026-09-10 12:35:06 +08:00
Catch PIL imread problems early and generate error message
This commit is contained in:
@@ -29,7 +29,11 @@ def imread(fname, dtype=None):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
im = Image.open(fname)
|
im = Image.open(fname)
|
||||||
return pil_to_ndarray(im, dtype)
|
try:
|
||||||
|
return pil_to_ndarray(im, dtype)
|
||||||
|
except IOError:
|
||||||
|
raise ValueError('Could not load "%s": make sure you have library '
|
||||||
|
'support for "%s" files' % (fname, im.format))
|
||||||
|
|
||||||
|
|
||||||
def pil_to_ndarray(im, dtype=None):
|
def pil_to_ndarray(im, dtype=None):
|
||||||
@@ -55,6 +59,8 @@ def pil_to_ndarray(im, dtype=None):
|
|||||||
im.shape = shape[::-1]
|
im.shape = shape[::-1]
|
||||||
elif 'A' in im.mode:
|
elif 'A' in im.mode:
|
||||||
im = im.convert('RGBA')
|
im = im.convert('RGBA')
|
||||||
|
# this will raise an IOError if the file is not readable
|
||||||
|
im.getdata()[0]
|
||||||
im = np.array(im, dtype=dtype)
|
im = np.array(im, dtype=dtype)
|
||||||
if fp is not None:
|
if fp is not None:
|
||||||
fp.close()
|
fp.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user