napari / napari-animation

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

Animate only image with no background #121

Open ptbrown1729 opened 1 year ago

ptbrown1729 commented 1 year ago

I have a 2D time series data which I want to animate, exporting only the image itself without extra background outside the image. Using canvas_only = False gets me part of the way there by removing any visible napari controls. But there is still lots of black background outside the image which is exported. Is there a way to remove the background entirely and only export the image in the video?

I can manually limit the background by adjusting the napari viewer so that the image itself is almost the only thing displayed and the amount of black background is limited. But I want to export the video programmatically without doing this.

I guess there is probably no reasonable way to do this and also support zooming/changing magnification. But it would be helpfully for time series data. Maybe the right approach is to programmatically adjust the napari display window aspect ratio/zoom to only display the image?

Here is an example script:

import numpy as np
import napari
from napari_animation import Animation

video_time = 20.
display_every_nth = 3
dxy = 0.065

# sample image
img = np.random.rand(3000, 501, 501)

# display
viewer = napari.Viewer()
viewer.add_image(img[::display_every_nth], colormap="magenta" scale=(dxy, dxy))

# animate
animation = Animation(viewer)
viewer.dims.set_current_step(0, 0)
animation.capture_keyframe()

nframes = imgr_roi[::display_every_nth].shape[0]
viewer.dims.set_current_step(0, nframes - 1)
animation.capture_keyframe(steps=nframes)

animation.animate("test.mp4", quality=6, canvas_only=True, fps=img.shape[::display_every_nth] / video_time)
Platform: Windows-10-10.0.19042-SP0
Python: 3.9.7 (default, Sep 16 2021, 16:59:28) [MSC v.1916 64 bit (AMD64)]
Qt: 5.15.4
PyQt5: 5.15.7
NumPy: 1.23.1
SciPy: 1.9.0
Dask: 2022.05.2
VisPy: 0.10.0

OpenGL:
- GL version: 4.6.0 NVIDIA 511.23
- MAX_TEXTURE_SIZE: 32768

Screens:
- screen 1: resolution 3440x1440, scale 1.0

Plugins:
- animation: 0.0.3.dev79+g54aa0e5
- console: 0.0.4
- micromanager: 0.0.1rc6.dev39+g98741c5.d20220812
- napari-svg: 0.1.6
- psfmodels: 0.3.2
- scikit-image: 0.4.16
imagesc-bot commented 1 year ago

This issue has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/making-napari-canvas-match-data-extents/70720/1

alisterburt commented 1 year ago

Hi @ptbrown1729 - your intuition about setting the window feels correct to me but I'm not immediately sure of the best way to do this in napari. I've asked a question over at image.sc (link above) and we should definitely add this as a feature when an answer is found!

GenevieveBuckley commented 1 year ago

Late to the party here, but I want to add one workaround for anyone else that stumbles on this issue.

If you're looking for an easy-ish way to make sure there's not lots of empty black background around your final animation, you can use either viewer.reset_view() at the beginning of your animation script, or set the viewer.camera.zoom and/or viewer.camera.angle.

I find that often gets me a good enough result, and is an easy thing to include at the start of animation scripts.