diff --git a/skimage/util/dtype.py b/skimage/util/dtype.py index 11199cc8..98d74167 100644 --- a/skimage/util/dtype.py +++ b/skimage/util/dtype.py @@ -63,6 +63,8 @@ def _convert(image, dtype): "%s to %s" % (dtypeobj_in, dtypeobj)) if kind_in == 'f': + if np.min(image) < 0 or np.max(image) > 1: + raise ValueError("Images of type float must be between 0 and 1") if kind == 'f': # floating point -> floating point if itemsize_in > itemsize: diff --git a/skimage/util/tests/test_dtype.py b/skimage/util/tests/test_dtype.py index dabfe044..c6b38d0f 100644 --- a/skimage/util/tests/test_dtype.py +++ b/skimage/util/tests/test_dtype.py @@ -67,6 +67,13 @@ def test_unsupported_dtype(): assert_raises(ValueError, img_as_int, x) +def test_float_out_of_range(): + too_high = np.array([2], dtype=np.float32) + assert_raises(ValueError, img_as_int, too_high) + too_low = np.array([-1], dtype=np.float32) + assert_raises(ValueError, img_as_int, too_low) + + if __name__ == '__main__': np.testing.run_module_suite()