Note
Go to the end to download the full example code.
Animate 2D#
Display a labels layer above of an image layer using the add_labels and add_image APIs
Rendering frames...
0%| | 0/166 [00:00<?, ?it/s]IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (1089, 630) to (1104, 640) to ensure video compatibility with most codecs and players. To prevent resizing, make your input image divisible by the macro_block_size or set the macro_block_size to 1 (risking incompatibility).
1%| | 1/166 [00:00<00:23, 7.01it/s]
3%|▎ | 5/166 [00:00<00:07, 22.31it/s]
6%|▌ | 10/166 [00:00<00:05, 31.00it/s]
9%|▉ | 15/166 [00:00<00:04, 36.32it/s]
12%|█▏ | 20/166 [00:00<00:03, 39.24it/s]
15%|█▌ | 25/166 [00:00<00:03, 40.36it/s]
18%|█▊ | 30/166 [00:00<00:03, 40.16it/s]
21%|██ | 35/166 [00:01<00:03, 34.54it/s]
23%|██▎ | 39/166 [00:01<00:04, 31.38it/s]
26%|██▌ | 43/166 [00:01<00:04, 29.36it/s]
28%|██▊ | 47/166 [00:01<00:04, 27.59it/s]
30%|███ | 50/166 [00:01<00:04, 26.77it/s]
32%|███▏ | 53/166 [00:01<00:04, 26.43it/s]
34%|███▎ | 56/166 [00:01<00:04, 26.16it/s]
36%|███▌ | 59/166 [00:01<00:04, 25.60it/s]
37%|███▋ | 62/166 [00:02<00:04, 25.46it/s]
39%|███▉ | 65/166 [00:02<00:03, 25.33it/s]
41%|████ | 68/166 [00:02<00:03, 25.52it/s]
43%|████▎ | 71/166 [00:02<00:03, 25.97it/s]
45%|████▍ | 74/166 [00:02<00:05, 16.66it/s]
46%|████▋ | 77/166 [00:02<00:04, 18.29it/s]
48%|████▊ | 80/166 [00:03<00:04, 19.83it/s]
50%|█████ | 83/166 [00:03<00:03, 21.11it/s]
52%|█████▏ | 86/166 [00:03<00:03, 22.14it/s]
54%|█████▎ | 89/166 [00:03<00:03, 22.85it/s]
55%|█████▌ | 92/166 [00:03<00:03, 24.48it/s]
58%|█████▊ | 96/166 [00:03<00:02, 26.66it/s]
60%|██████ | 100/166 [00:03<00:02, 27.93it/s]
63%|██████▎ | 104/166 [00:03<00:02, 28.67it/s]
64%|██████▍ | 107/166 [00:03<00:02, 28.94it/s]
67%|██████▋ | 111/166 [00:04<00:01, 29.61it/s]
69%|██████▉ | 115/166 [00:04<00:01, 29.89it/s]
72%|███████▏ | 119/166 [00:04<00:01, 30.41it/s]
74%|███████▍ | 123/166 [00:04<00:01, 30.12it/s]
77%|███████▋ | 127/166 [00:04<00:01, 30.60it/s]
79%|███████▉ | 131/166 [00:04<00:01, 31.02it/s]
81%|████████▏ | 135/166 [00:04<00:00, 31.47it/s]
84%|████████▎ | 139/166 [00:05<00:00, 31.60it/s]
86%|████████▌ | 143/166 [00:05<00:00, 31.35it/s]
89%|████████▊ | 147/166 [00:05<00:00, 30.91it/s]
91%|█████████ | 151/166 [00:05<00:00, 31.21it/s]
93%|█████████▎| 155/166 [00:05<00:00, 32.85it/s]
96%|█████████▌| 159/166 [00:05<00:00, 34.37it/s]
98%|█████████▊| 163/166 [00:05<00:00, 35.09it/s]
100%|██████████| 166/166 [00:05<00:00, 28.62it/s]
from skimage import data
from scipy import ndimage as ndi
from napari_animation import Animation
import napari
blobs = data.binary_blobs(length=128, volume_fraction=0.1, n_dim=3)
viewer = napari.Viewer()
viewer.add_image(blobs.astype(float), name="blobs")
labeled = ndi.label(blobs)[0]
viewer.add_labels(labeled, name="blob ID")
animation = Animation(viewer)
viewer.update_console({"animation": animation})
animation.capture_keyframe()
viewer.camera.zoom = 0.2
animation.capture_keyframe()
viewer.camera.zoom = 10.0
viewer.camera.center = (0, 40.0, 10.0)
animation.capture_keyframe()
viewer.dims.current_step = (60, 0, 0)
animation.capture_keyframe(steps=60)
viewer.dims.current_step = (0, 0, 0)
animation.capture_keyframe(steps=60)
viewer.reset_view()
animation.capture_keyframe()
animation.animate("animate2D.mp4", canvas_only=False)
Total running time of the script: (0 minutes 10.356 seconds)