def main(arg): #top = '/Users/decarlo/Desktop/util/elettra/img/' top = '/local/decarlo/conda/util/elettra/img/' print(imageio.help('JPEG-XR-FI')) # im = imageio.imread(top + "SMALLTOMATO.jxr") # imageio.imsave(top + "test.jxr", im) im = imageio.imread(top + "lena.tif") #imageio.imsave(top + "test_00.jxr", im) print(im.shape) imageio.imsave(top + "test_00.jxr", im, format='JXR') imageio.imsave(top + "test_01.tiff", im) imageio.imsave(top + "test_02.png", im) print( imageio.imsave(top + "test_03.jpeg", im, progressive=True, optimize=True, baseline=True)) imageio.imsave(top + "test_04.jpeg", im, flag='JPEG_QUALITYBAD')
def test_functions(): """ Test the user-facing API functions """ # Test help(), it prints stuff, so we just check whether that goes ok imageio.help() # should print overview imageio.help("PNG") # should print about PNG fname1 = get_remote_file("images/chelsea.png", test_dir) fname2 = fname1[:-3] + "jpg" fname3 = fname1[:-3] + "notavalidext" open(fname3, "wb") # Test read() R1 = imageio.read(fname1) R2 = imageio.read(fname1, "png") assert R1.format is R2.format # Fail raises(ValueError, imageio.read, fname3) # existing but not readable raises(FileNotFoundError, imageio.read, "notexisting.barf") raises(IndexError, imageio.read, fname1, "notexistingformat") # Test save() W1 = imageio.save(fname2) W2 = imageio.save(fname2, "JPG") W1.close() W2.close() assert W1.format is W2.format # Fail raises(FileNotFoundError, imageio.save, "~/dirdoesnotexist/wtf.notexistingfile") # Test imread() im1 = imageio.imread(fname1) im2 = imageio.imread(fname1, "png") assert im1.shape[2] == 3 assert np.all(im1 == im2) # Test imsave() if os.path.isfile(fname2): os.remove(fname2) assert not os.path.isfile(fname2) imageio.imsave(fname2, im1[:, :, 0]) imageio.imsave(fname2, im1) assert os.path.isfile(fname2) # Test mimread() fname3 = get_remote_file("images/newtonscradle.gif", test_dir) ims = imageio.mimread(fname3) assert isinstance(ims, list) assert len(ims) > 1 assert ims[0].ndim == 3 assert ims[0].shape[2] in (1, 3, 4) # Test protection with raises(RuntimeError): imageio.mimread("imageio:chelsea.png", "dummy", length=np.inf) if IS_PYPY: return # no support for npz format :( # Test mimsave() fname5 = fname3[:-4] + "2.npz" if os.path.isfile(fname5): os.remove(fname5) assert not os.path.isfile(fname5) imageio.mimsave(fname5, [im[:, :, 0] for im in ims]) imageio.mimsave(fname5, ims) assert os.path.isfile(fname5) # Test volread() fname4 = get_remote_file("images/stent.npz", test_dir) vol = imageio.volread(fname4) assert vol.ndim == 3 assert vol.shape[0] == 256 assert vol.shape[1] == 128 assert vol.shape[2] == 128 # Test volsave() volc = np.zeros((10, 10, 10, 3), np.uint8) # color volume fname6 = os.path.join(test_dir, "images", "stent2.npz") if os.path.isfile(fname6): os.remove(fname6) assert not os.path.isfile(fname6) imageio.volsave(fname6, volc) imageio.volsave(fname6, vol) assert os.path.isfile(fname6) # Test mvolread() vols = imageio.mvolread(fname4) assert isinstance(vols, list) assert len(vols) == 1 assert vols[0].shape == vol.shape # Test mvolsave() if os.path.isfile(fname6): os.remove(fname6) assert not os.path.isfile(fname6) imageio.mvolsave(fname6, [volc, volc]) imageio.mvolsave(fname6, vols) assert os.path.isfile(fname6) # Fail for save functions raises(ValueError, imageio.imsave, fname2, np.zeros((100, 100, 5))) raises(ValueError, imageio.imsave, fname2, 42) raises(ValueError, imageio.mimsave, fname5, [np.zeros((100, 100, 5))]) raises(ValueError, imageio.mimsave, fname5, [42]) raises(ValueError, imageio.volsave, fname6, np.zeros((100, 100, 100, 40))) raises(ValueError, imageio.volsave, fname6, 42) raises(ValueError, imageio.mvolsave, fname6, [np.zeros((90, 90, 90, 40))]) raises(ValueError, imageio.mvolsave, fname6, [42])
def test_functions(): """ Test the user-facing API functions """ # Test help(), it prints stuff, so we just check whether that goes ok imageio.help() # should print overview imageio.help('PNG') # should print about PNG fname1 = get_remote_file('images/chelsea.png', test_dir) fname2 = fname1[:-3] + 'jpg' fname3 = fname1[:-3] + 'notavalidext' open(fname3, 'wb') # Test read() R1 = imageio.read(fname1) R2 = imageio.read(fname1, 'png') assert R1.format is R2.format # Fail raises(ValueError, imageio.read, fname3) # existing but not readable raises(IOError, imageio.read, 'notexisting.barf') raises(IndexError, imageio.read, fname1, 'notexistingformat') # Test save() W1 = imageio.save(fname2) W2 = imageio.save(fname2, 'JPG') assert W1.format is W2.format # Fail raises(ValueError, imageio.save, 'wtf.notexistingfile') # Test imread() im1 = imageio.imread(fname1) im2 = imageio.imread(fname1, 'png') assert im1.shape[2] == 3 assert np.all(im1 == im2) # Test imsave() if os.path.isfile(fname2): os.remove(fname2) assert not os.path.isfile(fname2) imageio.imsave(fname2, im1[:, :, 0]) imageio.imsave(fname2, im1) assert os.path.isfile(fname2) # Test mimread() fname3 = get_remote_file('images/newtonscradle.gif', test_dir) ims = imageio.mimread(fname3) assert isinstance(ims, list) assert len(ims) > 1 assert ims[0].ndim == 3 assert ims[0].shape[2] in (1, 3, 4) if IS_PYPY: return # no support for npz format :( # Test mimsave() fname5 = fname3[:-4] + '2.npz' if os.path.isfile(fname5): os.remove(fname5) assert not os.path.isfile(fname5) imageio.mimsave(fname5, [im[:, :, 0] for im in ims]) imageio.mimsave(fname5, ims) assert os.path.isfile(fname5) # Test volread() fname4 = get_remote_file('images/stent.npz', test_dir) vol = imageio.volread(fname4) assert vol.ndim == 3 assert vol.shape[0] == 256 assert vol.shape[1] == 128 assert vol.shape[2] == 128 # Test volsave() volc = np.zeros((10, 10, 10, 3), np.uint8) # color volume fname6 = fname4[:-4] + '2.npz' if os.path.isfile(fname6): os.remove(fname6) assert not os.path.isfile(fname6) imageio.volsave(fname6, volc) imageio.volsave(fname6, vol) assert os.path.isfile(fname6) # Test mvolread() vols = imageio.mvolread(fname4) assert isinstance(vols, list) assert len(vols) == 1 assert vols[0].shape == vol.shape # Test mvolsave() if os.path.isfile(fname6): os.remove(fname6) assert not os.path.isfile(fname6) imageio.mvolsave(fname6, [volc, volc]) imageio.mvolsave(fname6, vols) assert os.path.isfile(fname6) # Fail for save functions raises(ValueError, imageio.imsave, fname2, np.zeros((100, 100, 5))) raises(ValueError, imageio.imsave, fname2, 42) raises(ValueError, imageio.mimsave, fname5, [np.zeros((100, 100, 5))]) raises(ValueError, imageio.mimsave, fname5, [42]) raises(ValueError, imageio.volsave, fname4, np.zeros((100, 100, 100, 40))) raises(ValueError, imageio.volsave, fname4, 42) raises(ValueError, imageio.mvolsave, fname4, [np.zeros((90, 90, 90, 40))]) raises(ValueError, imageio.mvolsave, fname4, [42])
def test_functions(test_images, tmp_path): """Test the user-facing API functions""" # Test help(), it prints stuff, so we just check whether that goes ok imageio.help() # should print overview imageio.help("PNG") # should print about PNG fname1 = test_images / "chelsea.png" fname2 = tmp_path / fname1.with_suffix(".jpg").name fname3 = tmp_path / fname1.with_suffix(".notavalidext").name open(fname3, "wb") # Test read() R1 = imageio.read(fname1) R2 = imageio.read(fname1, "png") # this tests if the highest priority png plugin and the highest # priority fallback plugin match. # Do we really what to enforce this? assert type(R1) is type(R2) raises(ValueError, imageio.read, fname3) # existing but not readable raises(IndexError, imageio.read, fname1, "notexistingformat") # Note: This is actually a test of Requests. We should probably # migrate or remove it. raises(FileNotFoundError, imageio.read, "notexisting.barf") # Test save() W1 = imageio.save(fname2) W2 = imageio.save(fname2, "JPG") W1.close() W2.close() assert type(W1) is type(W2) # Fail raises(FileNotFoundError, imageio.save, "~/dirdoesnotexist/wtf.notexistingfile") # Test imread() im1 = imageio.imread(fname1) im2 = imageio.imread(fname1, "png") assert im1.shape[2] == 3 assert np.all(im1 == im2) # Test imsave() if os.path.isfile(fname2): os.remove(fname2) assert not os.path.isfile(fname2) imageio.imsave(fname2, im1[:, :, 0]) imageio.imsave(fname2, im1) assert os.path.isfile(fname2) # Test mimread() fname3 = test_images / "newtonscradle.gif" ims = imageio.mimread(fname3) assert isinstance(ims, list) assert len(ims) > 1 assert ims[0].ndim == 3 assert ims[0].shape[2] in (1, 3, 4) # Test protection with raises(RuntimeError): imageio.mimread(test_images / "chelsea.png", "dummy", length=np.inf) if IS_PYPY: return # no support for npz format :( # Test mimsave() fname5 = str(fname3.with_suffix("")) fname5 += "2.npz" if os.path.isfile(fname5): os.remove(fname5) assert not os.path.isfile(fname5) imageio.mimsave(fname5, [im[:, :, 0] for im in ims]) imageio.mimsave(fname5, ims) assert os.path.isfile(fname5) # Test volread() fname4 = test_images / "stent.npz" vol = imageio.volread(fname4) assert vol.ndim == 3 assert vol.shape[0] == 256 assert vol.shape[1] == 128 assert vol.shape[2] == 128 # Test volsave() volc = np.zeros((10, 10, 10, 3), np.uint8) # color volume fname6 = tmp_path / "stent2.npz" if os.path.isfile(fname6): os.remove(fname6) assert not os.path.isfile(fname6) imageio.volsave(fname6, volc) imageio.volsave(fname6, vol) assert os.path.isfile(fname6) # Test mvolread() vols = imageio.mvolread(fname4) assert isinstance(vols, list) assert len(vols) == 1 assert vols[0].shape == vol.shape # Test mvolsave() if os.path.isfile(fname6): os.remove(fname6) assert not os.path.isfile(fname6) imageio.mvolsave(fname6, [volc, volc]) imageio.mvolsave(fname6, vols) assert os.path.isfile(fname6) # Fail for save functions raises(ValueError, imageio.imsave, fname2, np.zeros((100, 100, 5))) raises(ValueError, imageio.imsave, fname2, 42) raises(ValueError, imageio.mimsave, fname5, [np.zeros((100, 100, 5))]) raises(ValueError, imageio.mimsave, fname5, [42]) raises(ValueError, imageio.volsave, fname6, np.zeros((100, 100, 100, 40))) raises(ValueError, imageio.volsave, fname6, 42) raises(ValueError, imageio.mvolsave, fname6, [np.zeros((90, 90, 90, 40))]) raises(ValueError, imageio.mvolsave, fname6, [42])