diff --git a/scikits/image/io/pil_imread.py b/scikits/image/io/pil_imread.py index 641c61f4..421f45c7 100644 --- a/scikits/image/io/pil_imread.py +++ b/scikits/image/io/pil_imread.py @@ -10,7 +10,9 @@ def imread(fname, flatten=False, dtype=None): fname : string Image file name, e.g. ``test.jpg``. flatten : bool - If true, convert the output to grey-scale. + If True, convert color images to grey-scale. If `dtype` is not given, + converted color images are returned as 32-bit float images. + Images that are already in grey-scale format are not converted. dtype : dtype, optional NumPy data-type specifier. If given, the returned image has this type. If None (default), the data-type is determined automatically. @@ -32,6 +34,6 @@ def imread(fname, flatten=False, dtype=None): " instructions.") im = Image.open(fname) - if flatten: + if flatten and not im.mode in ('1', 'L', 'I', 'F', 'I;16', 'I;16L', 'I;16B'): im = im.convert('F') return np.array(im, dtype=dtype) diff --git a/scikits/image/io/tests/test_imread.py b/scikits/image/io/tests/test_imread.py index 3a68f9ee..86aa8fe6 100644 --- a/scikits/image/io/tests/test_imread.py +++ b/scikits/image/io/tests/test_imread.py @@ -4,7 +4,14 @@ import numpy as np from scikits.image import data_dir from scikits.image.io import imread -def test_imread(): - img = imread(os.path.join(data_dir, 'camera.png'), dtype=np.float32) - print img.dtype, type(img) +def test_imread_flatten(): + # a color image is flattened and returned as float32 + img = imread(os.path.join(data_dir, 'color.png'), flatten=True) assert img.dtype == np.float32 + img = imread(os.path.join(data_dir, 'camera.png'), flatten=True) + # check that flattening does not occur for an image that is grey already. + assert np.sctype2char(img.dtype) in np.typecodes['AllInteger'] + +def test_imread_dtype(): + img = imread(os.path.join(data_dir, 'camera.png'), dtype=np.float64) + assert img.dtype == np.float64