Old Examples#

based on https://github.com/napari/napari-animation/tree/main/examples

Background:
* Video are currently embedded into the notebook as bytestrings
[1]:
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)

[2]:
from napari_animation import Animation
from napari_animation.easing import Easing

# init the viewer. The same viewer can be used for all animations.
viewer = napari.view_image(data.cells3d(), channel_axis=1, ndisplay=3)


viewer.camera.angles = (0, 0, 90)

animation = Animation(viewer)
viewer.camera.zoom = 1
animation.capture_keyframe(steps=30)

viewer.camera.zoom = 2
animation.capture_keyframe(steps=60, ease=Easing.QUADRATIC)

viewer.camera.angles = (20, -25, 140)
animation.capture_keyframe(steps=60, ease=Easing.QUADRATIC)

viewer.camera.zoom = 1
animation.capture_keyframe(steps=60, ease=Easing.QUADRATIC)

viewer.camera.angles = (0, 0, 90)
animation.capture_keyframe(steps=60, ease=Easing.LINEAR)

jupyter_napari_animation(animation, canvas_only=True, fps=60)
Rendering frames...
  0%|          | 0/241 [00:00<?, ?it/s]IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (1058, 892) to (1072, 896) 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).
  0%|          | 1/241 [00:00<00:52,  4.54it/s][swscaler @ 0x7fe548008000] Warning: data is not aligned! This can lead to a speed loss
100%|██████████| 241/241 [00:07<00:00, 31.77it/s]
[3]:
"""
Display a labels layer above of an image layer using the add_labels and
add_image APIs
"""

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.reset_view()


animation.capture_keyframe()
viewer.camera.zoom = 0.8
animation.capture_keyframe()
viewer.camera.zoom = 3
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()

jupyter_napari_animation(animation, canvas_only=True, fps=60)
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 (1058, 892) to (1072, 896) 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).
[swscaler @ 0x7fa010048000] Warning: data is not aligned! This can lead to a speed loss
100%|██████████| 166/166 [00:06<00:00, 27.35it/s]
[4]:
"""
Display a labels layer above of an image layer using the add_labels and
add_image APIs
"""

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()
jupyter_napari_animation(animation, canvas_only=True, fps=60)
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 (1058, 892) to (1072, 896) 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).
[swscaler @ 0x7f99b8048000] Warning: data is not aligned! This can lead to a speed loss
100%|██████████| 151/151 [00:10<00:00, 14.77it/s]
[5]:
"""
Display a labels layer above of an image layer using the add_labels and
add_image APIs
"""

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.reset_view()

animation.capture_keyframe()
viewer.camera.zoom = 0.8
animation.capture_keyframe()
viewer.camera.zoom = 3
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()

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()
jupyter_napari_animation(animation, canvas_only=True, fps=60)
Rendering frames...
  0%|          | 0/331 [00:00<?, ?it/s]IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (1058, 994) to (1072, 1008) 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).
  0%|          | 1/331 [00:00<00:53,  6.18it/s][swscaler @ 0x7f8890008000] Warning: data is not aligned! This can lead to a speed loss
100%|██████████| 331/331 [00:18<00:00, 17.52it/s]
[6]:
import napari
from skimage import data
from napari_animation import Animation
from napari_animation.easing import Easing

cat = data.cat()
viewer = napari.view_image(cat, rgb=True)

animation = Animation(viewer)
viewer.camera.zoom = 1
animation.capture_keyframe(steps=0)
animation.capture_keyframe(steps=60)
viewer.camera.zoom = 1.5
animation.capture_keyframe(steps=30, ease=Easing.QUADRATIC)

animation.capture_keyframe(steps=60)

jupyter_napari_animation(animation, canvas_only=True, fps=60)
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 (1058, 1024) to (1072, 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).
[swscaler @ 0x7f8ea0008000] Warning: data is not aligned! This can lead to a speed loss
100%|██████████| 151/151 [00:03<00:00, 48.90it/s]