def test_broken_plugin(clear_plugins): imopen_module.known_plugins["plugin1"] = PluginConfig( name="plugin1", class_name="BrokenDummyPlugin", module_name="test_core") with pytest.raises(IOError): iio.imopen("", "r", plugin="plugin1", legacy_mode=False)
def test_plugin_selection_failure(clear_plugins, monkeypatch): class DummyPlugin: def __init__(self, request): raise InitializationError("Can not read anything") monkeypatch.setattr( iio.imopen, "_known_plugins", {"plugin1": DummyPlugin, "plugin2": DummyPlugin} ) with pytest.raises(IOError): iio.imopen("", "r", search_legacy_only=False)
def test_invalid_explicit_plugin(clear_plugins, monkeypatch): class DummyPlugin: def __init__(self, request): raise InitializationError("Can not read anything") monkeypatch.setattr( iio.imopen, "_known_plugins", {"plugin1": DummyPlugin, "plugin2": DummyPlugin} ) with pytest.raises(IOError): iio.imopen("", "r", plugin="plugin1")
def test_plugin_selection_failure(clear_plugins): imopen_module.known_plugins["plugin1"] = PluginConfig( name="plugin1", class_name="UselessDummyPlugin", module_name="test_core") imopen_module.known_plugins["plugin2"] = PluginConfig( name="plugin2", class_name="UselessDummyPlugin", module_name="test_core") with pytest.raises(IOError): iio.imopen("", "r", legacy_mode=False)
def test_exif_orientation(image_files: Path): from PIL.Image import Exif im = np.load(image_files / "chelsea.npy") # original image is has landscape format assert im.shape[0] < im.shape[1] im_flipped = np.rot90(im, -1) exif_tag = Exif() exif_tag[274] = 6 # Set Orientation to 6 iio.v3.imwrite(image_files / "chelsea_tagged.png", im_flipped, plugin="pillow", exif=exif_tag) with iio.imopen( image_files / "chelsea_tagged.png", "r", plugin="pillow", ) as f: im_reloaded = f.read() im_meta = f.get_meta() # ensure raw image is now portrait assert im_reloaded.shape[0] > im_reloaded.shape[1] # ensure that the Exif tag is set in the file assert "Orientation" in im_meta and im_meta["Orientation"] == 6 im_reloaded = iio.v3.imread(image_files / "chelsea_tagged.png", plugin="pillow", rotate=True) assert np.array_equal(im, im_reloaded)
def test_plugin_selection_success(clear_plugins, invalid_file): imopen_module.known_plugins["plugin"] = PluginConfig( name="plugin", class_name="EpicDummyPlugin", module_name="test_core") instance = iio.imopen(invalid_file, "r", legacy_mode=False) assert isinstance(instance, EpicDummyPlugin)
def test_imopen_installable_plugin(clear_plugins): # test uninstalled plugin iio.config.known_plugins["plugin"] = PluginConfig( name="plugin", class_name="EpicDummyPlugin", module_name="non_existant") with pytest.raises(IOError): iio.imopen("foo.bar", "w", legacy_mode=False) # register extension iio.config.known_extensions[".bar"] = [ FileExtension(extension=".bar", priority=["plugin"]) ] with pytest.raises(IOError): iio.imopen("foo.bar", "w", legacy_mode=False)
def test_gif_irregular_duration(image_files: Path): im = iio.v3.imread(image_files / "newtonscradle.gif", plugin="pillow", mode="RGBA") duration = [0.5 if idx in [2, 5, 7] else 0.1 for idx in range(im.shape[0])] with iio.imopen(image_files / "test.gif", "w", plugin="pillow") as file: for frame, duration in zip(im, duration): file.write(frame, duration=duration)
def test_plugin_selection_success(clear_plugins, monkeypatch, invalid_file): class DummyPlugin: def __init__(self, request): """Can read anything""" monkeypatch.setattr(iio.imopen, "_known_plugins", {"plugin1": DummyPlugin}) instance = iio.imopen(invalid_file, "r", search_legacy_only=False) assert isinstance(instance, DummyPlugin)
def test_gif_loop_and_fps(image_files: Path): # Note: I think this test tests pillow kwargs, not imageio functionality # maybe we should drop it? im = iio.v3.imread(image_files / "newtonscradle.gif", plugin="pillow", mode="RGBA") with iio.imopen(image_files / "test.gif", "w", plugin="pillow") as file: for frame in im: file.write(frame, palettesize=100, fps=20, loop=2)
def test_gif_indexed_read(image_files: Path): idx = 0 numpy_im = np.load(image_files / "newtonscradle_rgb.npy")[idx, ...] with iio.imopen(image_files / "newtonscradle.gif", "r", plugin="pillow") as file: # exists to touch branch, would be better two write an explicit test meta = file.get_meta(index=idx) assert "version" in meta pillow_im = file.read(index=idx, mode="RGB") assert np.allclose(pillow_im, numpy_im)
def test_png_gamma_correction(image_files: Path): with iio.imopen(image_files / "kodim03.png", "r", plugin="pillow") as f: im1 = f.read() im1_meta = f.get_meta() im2 = iio.v3.imread(image_files / "kodim03.png", plugin="pillow", apply_gamma=True) # Test result depending of application of gamma assert im1_meta["gamma"] < 1 assert im1.mean() < im2.mean() assert im1.shape == (512, 768, 3) assert im1.dtype == "uint8" assert im2.shape == (512, 768, 3) assert im2.dtype == "uint8"
def test_gif_legacy_pillow(test_images, im_in, mode): """ This test tests backwards compatibility of using the new API with a legacy plugin. IN particular reading ndimages I'm not sure where this test should live, so it is here for now. """ im_path = test_images / im_in with iio.imopen(im_path, "r", legacy_mode=True, plugin="GIF-PIL") as file: iio_im = file.read(pilmode=mode, index=None) pil_im = np.asarray([ np.array(frame.convert(mode)) for frame in ImageSequence.Iterator(Image.open(im_path)) ]) if pil_im.shape[0] == 1: pil_im = pil_im.squeeze(axis=0) assert np.allclose(iio_im, pil_im)
def test_legacy_exif_orientation(test_images, tmp_path): from PIL.Image import Exif im = np.load(test_images / "chelsea.npy") # original image is has landscape format assert im.shape[0] < im.shape[1] im_flipped = np.rot90(im, -1) exif_tag = Exif() exif_tag[274] = 6 # Set Orientation to 6 iio.v3.imwrite( tmp_path / "chelsea_tagged.png", im_flipped, plugin="pillow", exif=exif_tag, ) with iio.imopen( tmp_path / "chelsea_tagged.png", "r", legacy_mode=True, plugin="PNG-PIL", ) as f: im_reloaded = np.asarray(f.read()[0]) im_meta = f.get_meta() # ensure raw image is now portrait assert im_reloaded.shape[0] > im_reloaded.shape[1] # ensure that the Exif tag is set in the file assert "exif" in im_meta im_reloaded = iio.v3.imread(tmp_path / "chelsea_tagged.png", plugin="pillow", rotate=True) assert np.array_equal(im, im_reloaded)
def test_imopen_unregistered_plugin(clear_plugins, invalid_file): with pytest.raises(ValueError): iio.imopen(invalid_file, "r", plugin="unknown_plugin")
def test_legacy_empty_image(): with pytest.raises(RuntimeError): with iio.imopen("foo.bmp", "wI", format="GIF-PIL") as file: file.write([])
def test_imopen_unsupported_iomode(): with pytest.raises(ValueError): iio.imopen("", "unknown_iomode")
def test_imopen_no_plugin_found(clear_plugins): with pytest.raises(IOError): iio.imopen("unknown.abcd", "r", search_legacy_only=False)