Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Read all planes as one array - we call it a volume for clarity
vol = imageio.volread(filename1)
vols = imageio.mvolread(filename1)
assert vol.shape == (3, ) + im2.shape
assert len(vols) == 1 and vol.shape == vols[0].shape
for i in range(3):
assert (vol[i] == im2).all()
# remote multipage rgb file
filename2 = get_remote_file('images/multipage_rgb.tif')
img = imageio.mimread(filename2)
assert len(img) == 2
assert img[0].shape == (3, 10, 10)
# Mixed
W = imageio.save(filename1)
W.set_meta_data({'planarconfig': 'SEPARATE'}) # was "planar"
assert W.format.name == 'TIFF'
W.append_data(im2)
W.append_data(im2)
W.close()
#
R = imageio.read(filename1)
assert R.format.name == 'TIFF'
ims = list(R) # == [im for im in R]
assert (ims[0] == im2).all()
# meta = R.get_meta_data()
# assert meta['orientation'] == 'top_left' # not there in later version
# Fail
raises(IndexError, R.get_data, -1)
raises(IndexError, R.get_data, 3)
im33 = R.get_next_data()
R.set_image_index(frame1)
im31 = R.get_next_data()
for im in (im11, im12, im13, im21, im22, im23, im31, im32, im33):
assert im.shape == (720, 1280, 3)
assert (im11 == im21).all() and (im11 == im31).all()
assert (im12 == im22).all() and (im12 == im32).all()
assert (im13 == im23).all() and (im13 == im33).all()
assert not (im11 == im12).all()
assert not (im11 == im13).all()
# Save
with imageio.save(fname2, "ffmpeg") as W:
for im in ims1:
W.append_data(im)
# Read the result
ims2 = imageio.mimread(fname2, "ffmpeg")
assert len(ims1) == len(ims2)
for im in ims2:
assert im.shape == (720, 1280, 3)
# Check
for im1, im2 in zip(ims1, ims2):
diff = np.abs(im1.astype(np.float32) - im2.astype(np.float32))
if IS_PYPY:
assert (diff.sum() / diff.size) < 100
else:
assert diff.mean() < 2.5
# Multiple images
imageio.mimsave(filename1, [im2, im2, im2])
im = imageio.imread(filename1)
ims = imageio.mimread(filename1)
assert (im == im2).all()
assert len(ims) == 3
# Volumes
imageio.mvolsave(filename1, [im3, im3])
im = imageio.volread(filename1)
ims = imageio.mvolread(filename1)
assert (im == im3).all()
assert len(ims) == 2
# Mixed
W = imageio.save(filename1)
assert W.format.name == "NPZ"
W.append_data(im2)
W.append_data(im3)
W.append_data(im4)
raises(RuntimeError, W.set_meta_data, {}) # no meta data support
W.close()
#
R = imageio.read(filename1)
assert R.format.name == "NPZ"
ims = list(R) # == [im for im in R]
assert (ims[0] == im2).all()
assert (ims[1] == im3).all()
assert (ims[2] == im4).all()
# Fail
raises(IndexError, R.get_data, -1)
raises(IndexError, R.get_data, 3)
# We can also store grayscale
fname = fnamebase + ".animated.%i.gif" % 1
imageio.mimsave(fname, [x[:, :, 0] for x in ims], duration=0.2)
imageio.mimsave(fname, [x[:, :, :1] for x in ims], duration=0.2)
# Irragular duration. You probably want to check this manually (I did)
duration = [0.1 for i in ims]
for i in [2, 5, 7]:
duration[i] = 0.5
imageio.mimsave(fnamebase + ".animated_irr.gif", ims, duration=duration)
# Other parameters
imageio.mimsave(fnamebase + ".animated.loop2.gif", ims, loop=2, fps=20)
R = imageio.read(fnamebase + ".animated.loop2.gif")
W = imageio.save(fnamebase + ".animated.palettes100.gif", palettesize=100)
assert W._palettesize == 128
# Fail
raises(IndexError, R.get_meta_data, -1)
raises(ValueError, imageio.mimsave, fname, ims, palettesize=300)
raises(ValueError, imageio.mimsave, fname, ims, quantizer="foo")
raises(ValueError, imageio.mimsave, fname, ims, duration="foo")
# Add one duplicate image to ims to touch subractangle with not change
ims.append(ims[-1])
# Test subrectangles
imageio.mimsave(fnamebase + ".subno.gif", ims, subrectangles=False)
imageio.mimsave(fnamebase + ".subyes.gif", ims, subrectangles=True)
s1 = os.stat(fnamebase + ".subno.gif").st_size
s2 = os.stat(fnamebase + ".subyes.gif").st_size
assert s2 < s1
def test_read():
need_internet()
R = imageio.read(get_remote_file("images/cockatoo.mp4"), "avbin")
assert R.format is imageio.formats["avbin"]
fname = get_remote_file("images/cockatoo.mp4", force_download="2014-11-05")
reader = imageio.read(fname, "avbin")
assert reader.get_length() == 280
assert "fps" in reader.get_meta_data()
raises(Exception, imageio.save, "~/foo.mp4", "abin")
# assert not reader.format.can_write(core.Request('test.mp4', 'wI'))
for i in range(10):
im = reader.get_next_data()
assert im.shape == (720, 1280, 3)
# todo: fix this
# assert mean(im) > 100 and mean(im) < 115 KNOWN FAIL
# We can rewind
reader.get_data(0)
# But not seek
with raises(IndexError):
reader.get_data(4)
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 im.shape == (657, 451, 4)
assert mean(im) > 0
ims1.append(im)
# Seek
assert (R.get_data(3) == ims1[3]).all()
# Fails
raises(IndexError, R.get_data, -1) # No negative index
raises(IndexError, R.get_data, 10) # Out of bounds
R.close()
# Test loop
R = imageio.read(fname1, loop=True)
assert (R.get_data(10) == ims1[0]).all()
# setting meta data is ignored
W = imageio.save(fname2)
W.set_meta_data({"foo": 3})
W.close()
# Write and re-read, now without loop, and with html page
imageio.mimsave(fname2, ims1, loop=False, html=True)
ims2 = imageio.mimread(fname2)
# Check images. We can expect exact match, since
# SWF is lossless.
assert len(ims1) == len(ims2)
for im1, im2 in zip(ims1, ims2):
assert (im1 == im2).all()
# Test compressed
imageio.mimsave(fname3, ims2, compress=True)
ims3 = imageio.mimread(fname3)
""" Test reading and saveing tiff """
im2 = np.ones((10, 10, 3), np.uint8) * 2
filename1 = os.path.join(test_dir, "test_tiff.tiff")
# One image
imageio.imsave(filename1, im2, "itk")
im = imageio.imread(filename1, "itk")
print(im.shape)
ims = imageio.mimread(filename1, "itk")
print(im2.shape)
assert (im == im2).all()
assert len(ims) == 1
# Mixed
W = imageio.save(filename1, "itk")
raises(RuntimeError, W.set_meta_data, 1)
assert W.format.name == "ITK"
W.append_data(im2)
W.append_data(im2)
W.close()
#
R = imageio.read(filename1, "itk")
assert R.format.name == "ITK"
ims = list(R) # == [im for im in R]
assert (ims[0] == im2).all()
# Fail
raises(IndexError, R.get_data, -1)
raises(IndexError, R.get_data, 3)
raises(RuntimeError, R.get_meta_data)
"""
if colors is None:
colors = 256
logger = proglog.default_bar_logger(logger)
if not IMAGEIO_FOUND:
raise ImportError("Writing a gif with imageio requires ImageIO installed,"
" with e.g. 'pip install imageio'")
if fps is None:
fps = clip.fps
quantizer = 0 if opt != 0 else 'nq'
writer = imageio.save(
filename,
duration=1.0/fps,
quantizer=quantizer,
palettesize=colors,
loop=loop
)
logger(message='MoviePy - Building file %s with imageio.' % filename)
for frame in clip.iter_frames(fps=fps, logger=logger, dtype='uint8'):
writer.append_data(frame)
def writevideo(self,fps=25):
writer = imageio.save('foo.mp4', fps=25)
self.volumeRenderWidget.events.draw.connect(lambda e: writer.append_data(self.render()))
self.volumeRenderWidget.events.close.connect(lambda e: writer.close())