Bug fix for imread so images that are already grey-scale do not get flattened.

This commit is contained in:
Ralf Gommers
2009-10-21 16:39:48 +02:00
parent 2e29d257c3
commit 060916cd42
2 changed files with 14 additions and 5 deletions
+4 -2
View File
@@ -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)
+10 -3
View File
@@ -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