__all__ = ['imread'] import numpy as np try: from PIL import Image except ImportError: raise ImportError("The Python Image Library could not be found. " "Please refer to " "https://pypi.python.org/pypi/Pillow/ (or " "http://pypi.python.org/pypi/PIL/) " "for further instructions.") from skimage.util import img_as_ubyte from six import string_types def imread(fname, dtype=None): """Load an image from file. Parameters ---------- fname : str File name. dtype : numpy dtype object or string specifier Specifies data type of array elements. """ im = Image.open(fname) try: # this will raise an IOError if the file is not readable im.getdata()[0] except IOError: site = "http://pillow.readthedocs.org/en/latest/installation.html#external-libraries" raise ValueError('Could not load "%s"\nPlease see documentation at: %s' % (fname, site)) else: return pil_to_ndarray(im, dtype) def pil_to_ndarray(im, dtype=None): """Import a PIL Image object to an ndarray, in memory. Parameters ---------- Refer to ``imread``. """ fp = im.fp if hasattr(im, 'fp') else None if im.mode == 'P': if _palette_is_grayscale(im): im = im.convert('L') else: im = im.convert('RGB') elif im.mode == '1': im = im.convert('L') elif im.mode.startswith('I;16'): shape = im.size dtype = '>u2' if im.mode.endswith('B') else '