Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_different_read_modes():
dname1, dname2, fname1, fname2 = _prepare()
for fname, dname, n in [(fname1, dname1, 1), (fname2, dname2, 2)]:
# Test imread()
im = imageio.imread(fname)
assert isinstance(im, np.ndarray)
assert im.shape == (512, 512)
# Test mimread()
ims = imageio.mimread(fname)
assert isinstance(ims, list)
assert ims[0].shape == im.shape
assert len(ims) > 1
#
ims2 = imageio.mimread(dname)
assert len(ims) == len(ims2)
# Test volread()
vol = imageio.volread(dname)
assert vol.ndim == 3
assert vol.shape[0] > 10
assert vol.shape[1:] == (512, 512)
#
vol2 = imageio.volread(fname) # fname works as well
assert (vol == vol2).all()
def test_fits_reading():
""" Test reading fits """
need_internet() # We keep the fits files in the imageio-binary repo
if IS_PYPY:
return # no support for fits format :(
simple = get_remote_file("images/simple.fits")
multi = get_remote_file("images/multi.fits")
compressed = get_remote_file("images/compressed.fits.fz")
# One image
im = imageio.imread(simple)
ims = imageio.mimread(simple)
assert (im == ims[0]).all()
assert len(ims) == 1
# Multiple images
ims = imageio.mimread(multi)
assert len(ims) == 3
R = imageio.read(multi)
assert R.format.name == "FITS"
ims = list(R) # == [im for im in R]
assert len(ims) == 3
# Fail
raises = pytest.raises
raises(IndexError, R.get_data, -1)
raises(IndexError, R.get_data, 3)
im = im.copy()
im[:, -5:, 0] = i * 20
ims.append(im)
# Store - animated GIF always poops out RGB
for isfloat in (False, True):
for colors in (3, 4):
ims1 = ims[:]
if isfloat:
ims1 = [x.astype(np.float32) / 256 for x in ims1]
ims1 = [x[:, :, :colors] for x in ims1]
fname = fnamebase + ".animated.%i.gif" % colors
imageio.mimsave(fname, ims1, duration=0.2)
# Retrieve
print("fooo", fname, isfloat, colors)
ims2 = imageio.mimread(fname)
ims1 = [x[:, :, :3] for x in ims] # fresh ref
ims2 = [x[:, :, :3] for x in ims2] # discart alpha
for im1, im2 in zip(ims1, ims2):
assert_close(im1, im2, 1.1)
# 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)
for i in range(10):
im = im.copy()
im[:, -5:, 0] = i * 20
ims.append(im)
# Store - animated GIF always poops out RGB
for isfloat in (False, True):
for colors in (3, 4):
ims1 = ims[:]
if isfloat:
ims1 = [x.astype(np.float32) / 256 for x in ims1]
ims1 = [x[:, :, :colors] for x in ims1]
fname = fnamebase + ".animated.%i.gif" % colors
imageio.mimsave(fname, ims1, duration=0.2)
# Retrieve
ims2 = imageio.mimread(fname)
ims1 = [x[:, :, :3] for x in ims] # fresh ref
ims2 = [x[:, :, :3] for x in ims2] # discart alpha
for im1, im2 in zip(ims1, ims2):
assert_close(im1, im2, 1.1)
# 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)
def main():
gif = imageio.mimread(args.filename)
imgs = [cv2.cvtColor(img, cv2.COLOR_RGB2BGR) for img in gif]
#img = cv2.imread('butter.jpg')
while 1:
for img in imgs:
output = trans_color(img,args.threshold)
time.sleep(timeout)
generate(output)
if args.cpu:
gpu_ids = None
elif gpu_ids is None:
gpu_ids = [0]
if not args.gif:
# Read input image
image = cv2.imread(args.input)
# Process
result = process(image, gpu_ids, args.enablepubes)
# Write output image
cv2.imwrite(args.output, result)
else:
gif_imgs = imageio.mimread(args.input)
nums = len(gif_imgs)
print("Total {} frames in the gif!".format(nums))
tmp_dir = tempfile.mkdtemp()
process_gif(gif_imgs, gpu_ids, args.enablepubes, tmp_dir)
print("Creating gif")
imageio.mimsave(args.output if args.output != "output.png" else "output.gif",
[imageio.imread(os.path.join(tmp_dir, "output_{}.jpg".format(i))) for i in range(nums)])
shutil.rmtree(tmp_dir)
end = time.time()
duration = end - start
# Done
print("Done! We have taken", round(duration, 2), "seconds")
# Exit
image = io.imread(name)
if len(image.shape) == 2 or image.shape[2] == 1:
image = gray2rgb(image)
if image.shape[2] == 4:
image = image[..., :3]
image = img_as_float32(image)
video_array = np.moveaxis(image, 1, 0)
video_array = video_array.reshape((-1,) + image_shape)
video_array = np.moveaxis(video_array, 1, 2)
elif name.lower().endswith('.gif') or name.lower().endswith('.mp4') or name.lower().endswith('.mov'):
video = np.array(mimread(name))
if len(video.shape) == 3:
video = np.array([gray2rgb(frame) for frame in video])
if video.shape[-1] == 4:
video = video[..., :3]
video_array = img_as_float32(video)
else:
raise Exception("Unknown file extensions %s" % name)
return video_array