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 (1086, 635) to (1088, 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:25, 6.53it/s]
3%|▎ | 5/166 [00:00<00:07, 20.27it/s]
6%|▌ | 10/166 [00:00<00:05, 29.05it/s]
9%|▉ | 15/166 [00:00<00:04, 33.63it/s]
11%|█▏ | 19/166 [00:00<00:04, 35.48it/s]
14%|█▍ | 23/166 [00:00<00:03, 36.09it/s]
16%|█▋ | 27/166 [00:00<00:03, 35.17it/s]
19%|█▊ | 31/166 [00:00<00:03, 35.31it/s]
21%|██ | 35/166 [00:01<00:04, 30.06it/s]
23%|██▎ | 39/166 [00:01<00:04, 27.26it/s]
25%|██▌ | 42/166 [00:01<00:04, 25.95it/s]
27%|██▋ | 45/166 [00:01<00:05, 23.69it/s]
29%|██▉ | 48/166 [00:01<00:05, 23.00it/s]
31%|███ | 51/166 [00:01<00:04, 23.13it/s]
33%|███▎ | 54/166 [00:02<00:04, 22.65it/s]
34%|███▍ | 57/166 [00:02<00:04, 22.40it/s]
36%|███▌ | 60/166 [00:02<00:04, 22.54it/s]
38%|███▊ | 63/166 [00:02<00:04, 22.49it/s]
40%|███▉ | 66/166 [00:02<00:04, 22.45it/s]
42%|████▏ | 69/166 [00:02<00:04, 22.47it/s]
43%|████▎ | 72/166 [00:02<00:04, 22.71it/s]
45%|████▌ | 75/166 [00:02<00:04, 21.86it/s]
47%|████▋ | 78/166 [00:03<00:04, 21.84it/s]
49%|████▉ | 81/166 [00:03<00:03, 21.96it/s]
51%|█████ | 84/166 [00:03<00:03, 22.18it/s]
52%|█████▏ | 87/166 [00:03<00:03, 22.22it/s]
54%|█████▍ | 90/166 [00:03<00:03, 22.26it/s]
56%|█████▌ | 93/166 [00:03<00:03, 24.02it/s]
58%|█████▊ | 96/166 [00:03<00:02, 25.32it/s]
60%|█████▉ | 99/166 [00:03<00:02, 25.93it/s]
61%|██████▏ | 102/166 [00:04<00:02, 26.45it/s]
63%|██████▎ | 105/166 [00:04<00:02, 26.77it/s]
65%|██████▌ | 108/166 [00:04<00:02, 27.42it/s]
67%|██████▋ | 111/166 [00:04<00:01, 27.70it/s]
69%|██████▊ | 114/166 [00:04<00:01, 27.89it/s]
70%|███████ | 117/166 [00:04<00:01, 27.82it/s]
72%|███████▏ | 120/166 [00:04<00:01, 28.21it/s]
74%|███████▍ | 123/166 [00:04<00:01, 27.38it/s]
76%|███████▌ | 126/166 [00:04<00:01, 27.47it/s]
78%|███████▊ | 129/166 [00:05<00:01, 27.39it/s]
80%|███████▉ | 132/166 [00:05<00:01, 27.83it/s]
81%|████████▏ | 135/166 [00:05<00:01, 27.82it/s]
83%|████████▎ | 138/166 [00:05<00:01, 27.19it/s]
85%|████████▍ | 141/166 [00:05<00:00, 27.25it/s]
87%|████████▋ | 144/166 [00:05<00:00, 27.75it/s]
89%|████████▊ | 147/166 [00:05<00:00, 27.81it/s]
90%|█████████ | 150/166 [00:05<00:00, 27.81it/s]
93%|█████████▎| 154/166 [00:05<00:00, 29.04it/s]
95%|█████████▌| 158/166 [00:06<00:00, 30.31it/s]
98%|█████████▊| 162/166 [00:06<00:00, 30.98it/s]
100%|██████████| 166/166 [00:06<00:00, 31.67it/s]
100%|██████████| 166/166 [00:06<00:00, 26.44it/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 12.324 seconds)