Note
Go to the end to download the full example code.
Animate 3D#
Display a labels layer above of an image layer using the add_labels and add_image APIs
/home/runner/work/napari-animation/napari-animation/examples/animate3D.py:16: FutureWarning: `napari.view_image` is deprecated and will be removed in napari 0.7.0.
Use `viewer = napari.Viewer(); viewer.add_image(...)` instead.
viewer = napari.view_image(blobs.astype(float), name="blobs")
Rendering frames...
0%| | 0/151 [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/151 [00:00<00:26, 5.70it/s]
1%|▏ | 2/151 [00:00<00:22, 6.63it/s]
2%|▏ | 3/151 [00:00<00:20, 7.33it/s]
3%|▎ | 4/151 [00:00<00:18, 7.85it/s]
3%|▎ | 5/151 [00:00<00:17, 8.31it/s]
4%|▍ | 6/151 [00:00<00:16, 8.78it/s]
5%|▌ | 8/151 [00:00<00:14, 9.57it/s]
7%|▋ | 10/151 [00:01<00:13, 10.33it/s]
8%|▊ | 12/151 [00:01<00:12, 11.09it/s]
9%|▉ | 14/151 [00:01<00:11, 11.87it/s]
11%|█ | 16/151 [00:01<00:10, 12.82it/s]
12%|█▏ | 18/151 [00:01<00:09, 13.49it/s]
13%|█▎ | 20/151 [00:01<00:09, 13.94it/s]
15%|█▍ | 22/151 [00:01<00:08, 14.34it/s]
16%|█▌ | 24/151 [00:02<00:08, 14.57it/s]
17%|█▋ | 26/151 [00:02<00:08, 14.67it/s]
19%|█▊ | 28/151 [00:02<00:08, 14.78it/s]
20%|█▉ | 30/151 [00:02<00:08, 14.77it/s]
21%|██ | 32/151 [00:02<00:08, 14.78it/s]
23%|██▎ | 34/151 [00:02<00:07, 14.76it/s]
24%|██▍ | 36/151 [00:02<00:07, 14.72it/s]
25%|██▌ | 38/151 [00:03<00:07, 14.63it/s]
26%|██▋ | 40/151 [00:03<00:07, 14.57it/s]
28%|██▊ | 42/151 [00:03<00:07, 14.47it/s]
29%|██▉ | 44/151 [00:03<00:07, 13.88it/s]
30%|███ | 46/151 [00:03<00:07, 13.98it/s]
32%|███▏ | 48/151 [00:03<00:07, 13.77it/s]
33%|███▎ | 50/151 [00:03<00:07, 13.67it/s]
34%|███▍ | 52/151 [00:04<00:07, 13.58it/s]
36%|███▌ | 54/151 [00:04<00:07, 13.41it/s]
37%|███▋ | 56/151 [00:04<00:07, 13.43it/s]
38%|███▊ | 58/151 [00:04<00:06, 13.40it/s]
40%|███▉ | 60/151 [00:04<00:06, 13.49it/s]
41%|████ | 62/151 [00:04<00:06, 13.60it/s]
42%|████▏ | 64/151 [00:04<00:06, 13.55it/s]
44%|████▎ | 66/151 [00:05<00:06, 13.63it/s]
45%|████▌ | 68/151 [00:05<00:06, 13.43it/s]
46%|████▋ | 70/151 [00:05<00:05, 13.54it/s]
48%|████▊ | 72/151 [00:05<00:05, 13.52it/s]
49%|████▉ | 74/151 [00:05<00:05, 13.51it/s]
50%|█████ | 76/151 [00:05<00:05, 13.55it/s]
52%|█████▏ | 78/151 [00:05<00:05, 13.56it/s]
53%|█████▎ | 80/151 [00:06<00:05, 13.46it/s]
54%|█████▍ | 82/151 [00:06<00:05, 13.52it/s]
56%|█████▌ | 84/151 [00:06<00:04, 13.47it/s]
57%|█████▋ | 86/151 [00:06<00:04, 13.37it/s]
58%|█████▊ | 88/151 [00:06<00:04, 13.28it/s]
60%|█████▉ | 90/151 [00:06<00:04, 13.43it/s]
61%|██████ | 92/151 [00:07<00:04, 13.48it/s]
62%|██████▏ | 94/151 [00:07<00:04, 13.59it/s]
64%|██████▎ | 96/151 [00:07<00:04, 13.51it/s]
65%|██████▍ | 98/151 [00:07<00:03, 13.68it/s]
66%|██████▌ | 100/151 [00:07<00:03, 13.76it/s]
68%|██████▊ | 102/151 [00:07<00:03, 13.84it/s]
69%|██████▉ | 104/151 [00:07<00:03, 13.85it/s]
70%|███████ | 106/151 [00:08<00:03, 14.01it/s]
72%|███████▏ | 108/151 [00:08<00:03, 14.02it/s]
73%|███████▎ | 110/151 [00:08<00:02, 13.98it/s]
74%|███████▍ | 112/151 [00:08<00:02, 13.87it/s]
75%|███████▌ | 114/151 [00:08<00:02, 14.11it/s]
77%|███████▋ | 116/151 [00:08<00:02, 14.23it/s]
78%|███████▊ | 118/151 [00:08<00:02, 14.32it/s]
79%|███████▉ | 120/151 [00:09<00:02, 14.22it/s]
81%|████████ | 122/151 [00:09<00:02, 14.18it/s]
82%|████████▏ | 124/151 [00:09<00:01, 13.88it/s]
83%|████████▎ | 126/151 [00:09<00:01, 13.74it/s]
85%|████████▍ | 128/151 [00:09<00:01, 13.64it/s]
86%|████████▌ | 130/151 [00:09<00:01, 13.65it/s]
87%|████████▋ | 132/151 [00:09<00:01, 13.33it/s]
89%|████████▊ | 134/151 [00:10<00:01, 13.25it/s]
90%|█████████ | 136/151 [00:10<00:01, 13.32it/s]
91%|█████████▏| 138/151 [00:10<00:00, 13.08it/s]
93%|█████████▎| 140/151 [00:10<00:00, 12.56it/s]
94%|█████████▍| 142/151 [00:10<00:00, 12.03it/s]
95%|█████████▌| 144/151 [00:10<00:00, 11.13it/s]
97%|█████████▋| 146/151 [00:11<00:00, 10.29it/s]
98%|█████████▊| 148/151 [00:11<00:00, 9.47it/s]
99%|█████████▊| 149/151 [00:11<00:00, 9.06it/s]
99%|█████████▉| 150/151 [00:11<00:00, 8.66it/s]
100%|██████████| 151/151 [00:11<00:00, 8.40it/s]
100%|██████████| 151/151 [00:11<00:00, 12.78it/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.view_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})
viewer.dims.ndisplay = 3
viewer.camera.angles = (0.0, 0.0, 90.0)
animation.capture_keyframe()
viewer.camera.zoom = 2.4
animation.capture_keyframe()
viewer.camera.angles = (-7.0, 15.7, 62.4)
animation.capture_keyframe(steps=60)
viewer.camera.angles = (2.0, -24.4, -36.7)
animation.capture_keyframe(steps=60)
viewer.reset_view()
viewer.camera.angles = (0.0, 0.0, 90.0)
animation.capture_keyframe()
animation.animate("animate3D.mp4", canvas_only=False)
Total running time of the script: (0 minutes 16.028 seconds)