napari / napari-animation

A napari plugin for making animations
https://napari.github.io/napari-animation/
Other
74 stars 27 forks source link

Using Jupyter Output to display videos #147

Closed kolibril13 closed 1 year ago

kolibril13 commented 1 year ago

It would be nice to have a function or a cellmagic that displays the generated video directly in the Jupyter output!

Here is a prototype:

Cell1

import napari
from skimage import data
import numpy as np
from IPython.display import Video

cat = data.cat()

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

def jupyter_napari_animation(animation,**kwargs):
    from pathlib import Path
    from datetime import datetime

    dir = Path.cwd() / "media"
    dir.mkdir(exist_ok=True)
    mov_name =  "napari_video" + "@" + datetime.now().strftime("%Y-%m-%d@%H-%M-%S") + ".mp4"

    path = dir / mov_name
    animation.animate(path, **kwargs)
    #  ^ I know, the above line should be done differently! e.g. jupyter_napari_animation could be a method of the animation object.

    vid = Video(path, width=600, html_attributes=f"controls autoplay loop")
    display(vid)

Cell2

from napari_animation import Animation

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

jupyter_napari_animation(animation, canvas_only=True, fps=30)

This is inspired by the way manim videos are embedded:

https://github.com/ManimCommunity/manim/blob/3713ed6b1e0f9d51de8e74717bf4489c592f880f/manim/utils/ipython_magic.py#L176-L197

alisterburt commented 1 year ago

agree this would be cool! It looks like animate doesn't currently return anything - we could have the method return a small object which implements _ipython_display() and shows the video as you suggest! :)

I don't need this myself or use jupyter notebooks particularly heavily but would gladly accept a PR if you're keen to make this happen!

kolibril13 commented 1 year ago

Glad you like it! :)

It looks like animate doesn't currently return anything - we could have the method return a small object which implements _ipython_display() and shows the video as you suggest! :)

In theory, a good idea! In practive, I remember one issue that we had in manim is, that when you name the video yourself, e.g. animation.animate("my_vid.mov"), then the video did not update due to caching issues. Also, it can happen that users copy+paste their code into other cells, and then there are multiple my_vid.mov files. We solved this issue by using time-stemps in the file names.

When I find the time for it, I might make a pr :)