diff --git a/skimage/io/_plugins/imageio_plugin.ini b/skimage/io/_plugins/imageio_plugin.ini new file mode 100644 index 00000000..6429537b --- /dev/null +++ b/skimage/io/_plugins/imageio_plugin.ini @@ -0,0 +1,3 @@ +[imageio] +description = Image reading via the ImageIO Library +provides = imread, imsave diff --git a/skimage/io/_plugins/imageio_plugin.py b/skimage/io/_plugins/imageio_plugin.py new file mode 100644 index 00000000..d21df591 --- /dev/null +++ b/skimage/io/_plugins/imageio_plugin.py @@ -0,0 +1,3 @@ +__all__ = ['imread', 'imsave'] + +from imageio import imread, imsave diff --git a/skimage/io/tests/test_imageio.py b/skimage/io/tests/test_imageio.py new file mode 100644 index 00000000..36e863a9 --- /dev/null +++ b/skimage/io/tests/test_imageio.py @@ -0,0 +1,78 @@ +import os +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 +import skimage.io as sio + +try: + import imageio as _imageio +except ImportError: + imageio_available = False +else: + imageio_available = True + + +def setup(): + if imageio_available: + np.random.seed(0) + use_plugin('imageio') + + +def teardown(): + reset_plugins() + + +@skipif(not imageio_available) +def test_imageio_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 imageio_available) +def test_imageio_palette(): + img = imread(os.path.join(data_dir, 'palette_color.png')) + assert img.ndim == 3 + + +@skipif(not imageio_available) +def test_imageio_truncated_jpg(): + assert_raises((RuntimeError, ValueError), + sio.imread, + os.path.join(data_dir, 'truncated.jpg')) + + +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 imageio_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.rand(*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()