From 6ebc43bad8e1e94660ab7978cdd51098d125bb91 Mon Sep 17 00:00:00 2001 From: Luis Pedro Coelho Date: Mon, 5 Nov 2012 22:17:42 +0000 Subject: [PATCH 1/4] ENH Add imread io plugin This relies on imread (https://github.com/luispedro/imread) which supports a few microscopy formats otherwise hard to read (LSM and MetaMorph STK) as well as basic ones. --- skimage/io/_plugins/imread_plugin.ini | 3 + skimage/io/_plugins/imread_plugin.py | 43 ++++++++++++++ skimage/io/tests/test_imread.py | 85 +++++++++++++++++++++++++++ 3 files changed, 131 insertions(+) create mode 100644 skimage/io/_plugins/imread_plugin.ini create mode 100644 skimage/io/_plugins/imread_plugin.py create mode 100644 skimage/io/tests/test_imread.py diff --git a/skimage/io/_plugins/imread_plugin.ini b/skimage/io/_plugins/imread_plugin.ini new file mode 100644 index 00000000..a6a5ddb1 --- /dev/null +++ b/skimage/io/_plugins/imread_plugin.ini @@ -0,0 +1,3 @@ +[imread] +description = Image reading and writing via imread +provides = imread, imsave diff --git a/skimage/io/_plugins/imread_plugin.py b/skimage/io/_plugins/imread_plugin.py new file mode 100644 index 00000000..7e7681b0 --- /dev/null +++ b/skimage/io/_plugins/imread_plugin.py @@ -0,0 +1,43 @@ +__all__ = ['imread', 'imsave'] + +import numpy as np + +try: + import imread as _imread +except ImportError: + raise ImportError("Imread could not be found" + "Please refer to http://pypi.python.org/pypi/imread/ " + "for further instructions.") + +def imread(fname, dtype=None): + """Load an image from file. + + Parameters + ---------- + fname : str + Name of input file + + """ + im = _imread.imread(fname) + if dtype is not None: + im = im.asdtype(dtype) + return im + +def imsave(fname, arr, format_str=None): + """Save an image to disk. + + Parameters + ---------- + fname : str + Name of destination file. + arr : ndarray of uint8 or uint16 + Array (image) to save. + format_str: str,optional + Format to save as. + + Notes + ----- + Currently, only 8-bit precision is supported. + """ + return _imread.imsave(fname, arr, formatstr=format_str) + diff --git a/skimage/io/tests/test_imread.py b/skimage/io/tests/test_imread.py new file mode 100644 index 00000000..8dd6ea6f --- /dev/null +++ b/skimage/io/tests/test_imread.py @@ -0,0 +1,85 @@ +import os.path +import numpy as np +from numpy.testing import * +from numpy.testing.decorators import skipif + +from tempfile import NamedTemporaryFile + +from skimage import data_dir +from skimage.io import imread, imsave, use_plugin, reset_plugins + +try: + import imread as _imread + use_plugin('imread') +except ImportError: + imread_available = False +else: + imread_available = True + + +def teardown(): + reset_plugins() + + +def setup_module(self): + """The effect of the `plugin.use` call may be overridden by later imports. + Call `use_plugin` directly before the tests to ensure that imread is used. + + """ + try: + use_plugin('imread') + except ImportError: + pass + + + +@skipif(not imread_available) +def test_imread_flatten(): + # a color image is flattened + img = imread(os.path.join(data_dir, 'color.png'), flatten=True) + assert img.ndim == 2 + assert img.dtype == np.float64 + 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'] + + +@skipif(not imread_available) +def test_imread_palette(): + img = imread(os.path.join(data_dir, 'palette_color.png')) + assert img.ndim == 3 + + +@skipif(not imread_available) +def test_bilevel(): + expected = np.zeros((10, 10), bool) + expected[::2] = 1 + + img = imread(os.path.join(data_dir, 'checker_bilevel.png')) + assert_array_equal(img, expected) + + +class TestSave: + def roundtrip(self, x, scaling=1): + f = NamedTemporaryFile(suffix='.png') + fname = f.name + f.close() + imsave(fname, x) + y = imread(fname) + + assert_array_almost_equal((x * scaling).astype(np.int32), y) + + @skipif(not imread_available) + def test_imsave_roundtrip(self): + dtype = np.uint8 + for shape in [(10, 10), (10, 10, 3), (10, 10, 4)]: + x = np.ones(shape, dtype=dtype) * np.random.random(shape) + + if np.issubdtype(dtype, float): + yield self.roundtrip, x, 255 + else: + x = (x * 255).astype(dtype) + yield self.roundtrip, x + +if __name__ == "__main__": + run_module_suite() From 10ce9b89abaf7f2d9bb914771bb77d7f1ded55f5 Mon Sep 17 00:00:00 2001 From: Luis Pedro Coelho Date: Mon, 5 Nov 2012 22:32:43 +0000 Subject: [PATCH 2/4] RFCT Use `skimage.utils.convert` for type conversion --- skimage/io/_plugins/imread_plugin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/skimage/io/_plugins/imread_plugin.py b/skimage/io/_plugins/imread_plugin.py index 7e7681b0..323fbec8 100644 --- a/skimage/io/_plugins/imread_plugin.py +++ b/skimage/io/_plugins/imread_plugin.py @@ -1,6 +1,7 @@ __all__ = ['imread', 'imsave'] import numpy as np +from skimage.utils.dtype import convert try: import imread as _imread @@ -20,7 +21,7 @@ def imread(fname, dtype=None): """ im = _imread.imread(fname) if dtype is not None: - im = im.asdtype(dtype) + im = convert(im, dtype) return im def imsave(fname, arr, format_str=None): From 1f84c5e35fb6bc981af7c2938e3a997e2d965b6c Mon Sep 17 00:00:00 2001 From: Luis Pedro Coelho Date: Mon, 5 Nov 2012 22:33:40 +0000 Subject: [PATCH 3/4] DOC Add myself to CONTRIBUTORS.txt --- CONTRIBUTORS.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 8ba62e0b..012fd53d 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -119,4 +119,7 @@ Perimeter calculation in regionprops. - Olivier Debeir - Rank filters (8- and 16-bits) using sliding window. \ No newline at end of file + Rank filters (8- and 16-bits) using sliding window. + +- Luis Pedro Coelho + imread plugin From 38f699471eab29259a075bcc52fef4514aed81d7 Mon Sep 17 00:00:00 2001 From: Luis Pedro Coelho Date: Sat, 24 Nov 2012 11:12:17 +0000 Subject: [PATCH 4/4] TST Remove unneeded test Per stefanv's comment on github, this was testing functionality higher up in skimage.io --- skimage/io/tests/test_imread.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/skimage/io/tests/test_imread.py b/skimage/io/tests/test_imread.py index 8dd6ea6f..afb9eac9 100644 --- a/skimage/io/tests/test_imread.py +++ b/skimage/io/tests/test_imread.py @@ -21,18 +21,6 @@ def teardown(): reset_plugins() -def setup_module(self): - """The effect of the `plugin.use` call may be overridden by later imports. - Call `use_plugin` directly before the tests to ensure that imread is used. - - """ - try: - use_plugin('imread') - except ImportError: - pass - - - @skipif(not imread_available) def test_imread_flatten(): # a color image is flattened