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
/home/runner/work/napari-animation/napari-animation/examples/animate2D.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/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.43it/s]
3%|▎ | 5/166 [00:00<00:08, 19.92it/s]
5%|▍ | 8/166 [00:00<00:12, 12.77it/s]
8%|▊ | 13/166 [00:00<00:07, 20.06it/s]
11%|█ | 18/166 [00:00<00:05, 25.55it/s]
13%|█▎ | 22/166 [00:00<00:05, 28.52it/s]
16%|█▌ | 26/166 [00:01<00:04, 30.13it/s]
18%|█▊ | 30/166 [00:01<00:04, 29.97it/s]
20%|██ | 34/166 [00:01<00:04, 28.00it/s]
22%|██▏ | 37/166 [00:01<00:04, 26.25it/s]
24%|██▍ | 40/166 [00:01<00:05, 25.05it/s]
26%|██▌ | 43/166 [00:01<00:05, 23.10it/s]
28%|██▊ | 46/166 [00:01<00:05, 22.70it/s]
30%|██▉ | 49/166 [00:02<00:05, 22.26it/s]
31%|███▏ | 52/166 [00:02<00:05, 22.47it/s]
33%|███▎ | 55/166 [00:02<00:05, 22.18it/s]
35%|███▍ | 58/166 [00:02<00:04, 22.03it/s]
37%|███▋ | 61/166 [00:02<00:04, 21.70it/s]
39%|███▊ | 64/166 [00:02<00:04, 21.80it/s]
40%|████ | 67/166 [00:02<00:04, 21.72it/s]
42%|████▏ | 70/166 [00:03<00:04, 21.87it/s]
44%|████▍ | 73/166 [00:03<00:04, 22.28it/s]
46%|████▌ | 76/166 [00:03<00:04, 21.94it/s]
48%|████▊ | 79/166 [00:03<00:03, 21.83it/s]
49%|████▉ | 82/166 [00:03<00:03, 21.90it/s]
51%|█████ | 85/166 [00:03<00:03, 21.86it/s]
53%|█████▎ | 88/166 [00:03<00:03, 21.97it/s]
55%|█████▍ | 91/166 [00:03<00:03, 22.42it/s]
57%|█████▋ | 94/166 [00:04<00:03, 23.68it/s]
58%|█████▊ | 97/166 [00:04<00:02, 24.60it/s]
60%|██████ | 100/166 [00:04<00:02, 25.48it/s]
62%|██████▏ | 103/166 [00:04<00:02, 26.21it/s]
64%|██████▍ | 106/166 [00:04<00:02, 26.22it/s]
66%|██████▌ | 109/166 [00:04<00:02, 26.59it/s]
67%|██████▋ | 112/166 [00:04<00:02, 26.81it/s]
69%|██████▉ | 115/166 [00:04<00:01, 26.81it/s]
71%|███████ | 118/166 [00:04<00:01, 26.95it/s]
73%|███████▎ | 121/166 [00:05<00:01, 26.93it/s]
75%|███████▍ | 124/166 [00:05<00:01, 26.56it/s]
77%|███████▋ | 127/166 [00:05<00:01, 26.61it/s]
78%|███████▊ | 130/166 [00:05<00:01, 26.83it/s]
80%|████████ | 133/166 [00:05<00:01, 26.52it/s]
82%|████████▏ | 136/166 [00:05<00:01, 26.66it/s]
84%|████████▎ | 139/166 [00:05<00:01, 26.80it/s]
86%|████████▌ | 142/166 [00:05<00:00, 26.87it/s]
87%|████████▋ | 145/166 [00:05<00:00, 26.94it/s]
89%|████████▉ | 148/166 [00:06<00:00, 27.06it/s]
91%|█████████ | 151/166 [00:06<00:00, 26.62it/s]
93%|█████████▎| 155/166 [00:06<00:00, 28.32it/s]
96%|█████████▌| 159/166 [00:06<00:00, 29.67it/s]
98%|█████████▊| 162/166 [00:06<00:00, 29.62it/s]
100%|██████████| 166/166 [00:06<00:00, 30.34it/s]
100%|██████████| 166/166 [00:06<00:00, 24.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})
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 14.389 seconds)