Animation Gallery#
[19]:
import napari
from skimage import data
# helper function to dispaly the animation in the notebook
from IPython.display import Video, display
def jupyter_napari_animation(animation, **kwargs):
from pathlib import Path
from datetime import datetime
dir = Path.cwd() / "media"
dir.mkdir(exist_ok=True)
timestamp = datetime.now().strftime("%Y-%m-%d@%H-%M-%S")
mov_name = "napari_video" + "@" + timestamp + ".mp4"
path = dir / mov_name
animation.animate(path, **kwargs)
vid = Video(path, width=600, html_attributes=f"controls autoplay loop" , embed=True)
display(vid)
viewer = napari.Viewer()
viewer.theme = "light"
viewer.dims.ndisplay = 3
blobs = data.binary_blobs(length=301, volume_fraction=0.1, n_dim=3, seed=42)
blobs = blobs[200:, 200:, :-100]
blobs[:, :, :50] = 1
viewer.reset_view()
viewer.layers.clear()
viewer.dims.ndisplay = 3
layer1 = viewer.add_labels(blobs, name="layer1", seed=0.03)
layer1.opacity = 1
sec = 23
[20]:
viewer.window.resize(800, 500)
viewer.window.qt_viewer.dockLayerControls.toggleViewAction().trigger() # NOT
viewer.window.qt_viewer.dockLayerList.toggleViewAction().trigger() # NOT
Public access to Window.qt_viewer is deprecated and will be removed in
v0.5.0. It is considered an "implementation detail" of the napari
application, not part of the napari viewer model. If your use case
requires access to qt_viewer, please open an issue to discuss.
Public access to Window.qt_viewer is deprecated and will be removed in
v0.5.0. It is considered an "implementation detail" of the napari
application, not part of the napari viewer model. If your use case
requires access to qt_viewer, please open an issue to discuss.
Keyframes#
Transitions#
[22]:
from napari_animation import Animation
viewer.reset_view()
animation = Animation(viewer)
viewer.camera.zoom = 2
animation.capture_keyframe(steps=0)
viewer.camera.zoom = 3.5
animation.capture_keyframe(steps=3*sec)
jupyter_napari_animation(animation, canvas_only=True, fps=sec)
Rendering frames...
0%| | 0/70 [00:00<?, ?it/s]WARNING:imageio_ffmpeg:IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (1580, 1016) to (1584, 1024) 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).
100%|██████████| 70/70 [00:04<00:00, 14.82it/s]
[23]:
viewer.reset_view()
viewer.camera.zoom = 3.5
animation = Animation(viewer)
viewer.camera.angles = (0, 0, 90)
animation.capture_keyframe(steps=0)
viewer.camera.angles = (0, 0, 0)
animation.capture_keyframe(steps=3*sec)
jupyter_napari_animation(animation, canvas_only=True, fps=sec)
Rendering frames...
0%| | 0/70 [00:00<?, ?it/s]WARNING:imageio_ffmpeg:IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (1580, 1016) to (1584, 1024) 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).
100%|██████████| 70/70 [00:05<00:00, 13.97it/s]
[24]:
viewer.reset_view()
viewer.camera.zoom = 3.5
animation = Animation(viewer)
animation.capture_keyframe(steps=0)
viewer.camera.center = (50, 50, 0)
animation.capture_keyframe(steps=3*sec)
jupyter_napari_animation(animation, canvas_only=True, fps=sec)
Rendering frames...
0%| | 0/70 [00:00<?, ?it/s]WARNING:imageio_ffmpeg:IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (1580, 1016) to (1584, 1024) 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).
100%|██████████| 70/70 [00:05<00:00, 13.81it/s]
[27]:
# making a pause
viewer.reset_view()
viewer.camera.zoom = 3.5
animation = Animation(viewer)
animation.capture_keyframe(steps=0)
viewer.camera.angles = (45, 0, 90)
animation.capture_keyframe(steps=1*sec)
animation.capture_keyframe(steps=1*sec) # this is a pause
viewer.camera.angles = (90, 0, 90)
animation.capture_keyframe(steps=1*sec)
jupyter_napari_animation(animation, canvas_only=True, fps=sec)
Rendering frames...
0%| | 0/70 [00:00<?, ?it/s]WARNING:imageio_ffmpeg:IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (1580, 1016) to (1584, 1024) 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).
100%|██████████| 70/70 [00:05<00:00, 13.72it/s]
[40]:
viewer.reset_view()
viewer.camera.zoom = 3.5
viewer.dims.ndisplay = 2
animation = Animation(viewer)
viewer.dims.current_step = [30, 0, 0]
animation.capture_keyframe(steps=0)
viewer.dims.current_step = [100, 0, 0]
animation.capture_keyframe(steps=3*sec)
jupyter_napari_animation(animation, canvas_only=True, fps=sec)
Rendering frames...
0%| | 0/70 [00:00<?, ?it/s]WARNING:imageio_ffmpeg:IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (1580, 986) to (1584, 992) 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).
100%|██████████| 70/70 [00:02<00:00, 24.95it/s]
Rate functions#
[62]:
from napari_animation.easing import Easing
viewer.reset_view()
viewer.camera.zoom = 3.5
viewer.dims.ndisplay = 3
animation = Animation(viewer)
viewer.camera.angles = (0, 0, 90)
animation.capture_keyframe(steps=0)
viewer.camera.angles = (30, 0, 90)
animation.capture_keyframe(steps=sec, ease=Easing.QUADRATIC)
viewer.camera.angles = (60, 0, 90)
animation.capture_keyframe(steps=sec, ease=Easing.QUADRATIC)
viewer.camera.angles = (90, 0, 90)
animation.capture_keyframe(steps=sec, ease=Easing.EXPONENTIAL)
jupyter_napari_animation(animation, canvas_only=True, fps=sec)
Rendering frames...
0%| | 0/70 [00:00<?, ?it/s]WARNING:imageio_ffmpeg:IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (1580, 1016) to (1584, 1024) 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).
100%|██████████| 70/70 [00:05<00:00, 13.31it/s]
[63]:
# print values of the enum easing
for easing in Easing:
print(easing)
Easing.LINEAR
Easing.QUADRATIC
Easing.CUBIC
Easing.QUINTIC
Easing.SINE
Easing.CIRCULAR
Easing.EXPONENTIAL
Easing.ELASTIC
Easing.BACK
Easing.BOUNCE
Framerate#
[68]:
sec = 60
viewer.reset_view()
viewer.camera.zoom = 3.5
animation = Animation(viewer)
viewer.camera.angles = (0, 0, 90)
animation.capture_keyframe(steps=0)
viewer.camera.angles = (45, 45, 45)
animation.capture_keyframe(steps=3*60)
jupyter_napari_animation(animation, canvas_only=True, fps=sec)
Rendering frames...
0%| | 0/181 [00:00<?, ?it/s]WARNING:imageio_ffmpeg:IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (1580, 1016) to (1584, 1024) 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).
100%|██████████| 181/181 [00:13<00:00, 13.13it/s]
[67]:
sec = 5
viewer.reset_view()
viewer.camera.zoom = 3.5
animation = Animation(viewer)
viewer.camera.angles = (0, 0, 90)
animation.capture_keyframe(steps=0)
viewer.camera.angles = (45, 45, 45)
animation.capture_keyframe(steps=3*sec)
jupyter_napari_animation(animation, canvas_only=True, fps=sec)
Rendering frames...
0%| | 0/16 [00:00<?, ?it/s]WARNING:imageio_ffmpeg:IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (1580, 1016) to (1584, 1024) 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).
100%|██████████| 16/16 [00:01<00:00, 11.89it/s]
[ ]: