diff --git a/skimage/io/_plugins/tifffile_plugin.ini b/skimage/io/_plugins/tifffile_plugin.ini new file mode 100644 index 00000000..4666e503 --- /dev/null +++ b/skimage/io/_plugins/tifffile_plugin.ini @@ -0,0 +1,3 @@ +[tifffile] +description = Open and save TIFF and TIFF-based (LSM, STK, etc.) images using tifffile.py +provides = imread, imsave diff --git a/skimage/io/_plugins/tifffile_plugin.py b/skimage/io/_plugins/tifffile_plugin.py new file mode 100644 index 00000000..a5688bd4 --- /dev/null +++ b/skimage/io/_plugins/tifffile_plugin.py @@ -0,0 +1,6 @@ +try: + from tifffile import imread, imsave +except ImportError: + raise ImportError("The tifffile module could not be found.\n" + "It can be obtained at " + "\n") diff --git a/skimage/io/tests/test_freeimage.py b/skimage/io/tests/test_freeimage.py index 4cbf9830..05ce5258 100644 --- a/skimage/io/tests/test_freeimage.py +++ b/skimage/io/tests/test_freeimage.py @@ -1,23 +1,66 @@ import os import skimage as si import skimage.io as sio +import numpy as np from numpy.testing import * from numpy.testing.decorators import skipif +from tempfile import NamedTemporaryFile try: import skimage.io._plugins.freeimage_plugin as fi FI_available = True + sio.use_plugin('freeimage') except OSError: FI_available = False @skipif(not FI_available) -def test_read(): - sio.use_plugin('freeimage', 'imread') +def test_imread(): img = sio.imread(os.path.join(si.data_dir, 'color.png')) assert img.shape == (370, 371, 3) assert all(img[274,135] == [0, 130, 253]) +@skipif(not FI_available) +def test_imread_uint16(): + expected = np.load(os.path.join(si.data_dir, 'chessboard_GRAY_U8.npy')) + img = sio.imread(os.path.join(si.data_dir, 'chessboard_GRAY_U16.tif')) + assert img.dtype == np.uint16 + assert_array_almost_equal(img, expected) + +@skipif(not FI_available) +def test_imread_uint16_big_endian(): + expected = np.load(os.path.join(si.data_dir, 'chessboard_GRAY_U8.npy')) + img = sio.imread(os.path.join(si.data_dir, 'chessboard_GRAY_U16B.tif')) + assert img.dtype == np.uint16 + assert_array_almost_equal(img, expected) + + +class TestSave: + def roundtrip(self, dtype, x, suffix): + print dtype, x.shape, suffix + f = NamedTemporaryFile(suffix='.'+suffix) + fname = f.name + f.close() + sio.imsave(fname, x) + y = sio.imread(fname) + assert_array_equal(x, y) + + @skipif(not FI_available) + def test_imsave_roundtrip(self): + for shape, dtype, format in [ + [(10, 10), (np.uint8, np.uint16), ('tif', 'png')], + [(10, 10), (np.float32,), ('tif',)], + [(10, 10, 3), (np.uint8,), ('png',)], + [(10, 10, 4), (np.uint8,), ('png',)] + ]: + tests = [(d,f) for d in dtype for f in format] + for d, f in tests: + x = np.ones(shape, dtype=d) * np.random.random(shape) + if not np.issubdtype(d, float): + x = (x * 255).astype(d) + yield self.roundtrip, d, x, f + + @skipif(not FI_available) def test_metadata(): meta = fi.read_metadata(os.path.join(si.data_dir, 'multipage.tif')) diff --git a/skimage/io/tests/test_tifffile.py b/skimage/io/tests/test_tifffile.py new file mode 100644 index 00000000..e412cd93 --- /dev/null +++ b/skimage/io/tests/test_tifffile.py @@ -0,0 +1,53 @@ +import os +import skimage as si +import skimage.io as sio +import numpy as np + +from numpy.testing import * +from numpy.testing.decorators import skipif +from tempfile import NamedTemporaryFile + +try: + import skimage.io._plugins.tifffile_plugin as tf + TF_available = True + sio.use_plugin('tifffile') +except OSError: + TF_available = False + +@skipif(not TF_available) +def test_imread_uint16(): + expected = np.load(os.path.join(si.data_dir, 'chessboard_GRAY_U8.npy')) + img = sio.imread(os.path.join(si.data_dir, 'chessboard_GRAY_U16.tif')) + assert img.dtype == np.uint16 + assert_array_almost_equal(img, expected) + +@skipif(not TF_available) +def test_imread_uint16_big_endian(): + expected = np.load(os.path.join(si.data_dir, 'chessboard_GRAY_U8.npy')) + img = sio.imread(os.path.join(si.data_dir, 'chessboard_GRAY_U16B.tif')) + assert img.dtype == np.uint16 + assert_array_almost_equal(img, expected) + + +class TestSave: + def roundtrip(self, dtype, x): + f = NamedTemporaryFile(suffix='.tif') + fname = f.name + f.close() + sio.imsave(fname, x) + y = sio.imread(fname) + assert_array_equal(x, y) + + @skipif(not TF_available) + def test_imsave_roundtrip(self): + for shape in [(10, 10), (10, 10, 3), (10, 10, 4)]: + for dtype in (np.uint8, np.uint16, np.float32, np.float64): + x = np.ones(shape, dtype=dtype) * np.random.random(shape) + + if not np.issubdtype(dtype, float): + x = (x * 255).astype(dtype) + yield self.roundtrip, dtype, x + + +if __name__ == "__main__": + run_module_suite()