Interactive image visualization with napari#
napari is a python-based image viewer. Today, we will use it by remote-controlling it from a jupyter notebook.
See also
For opening an image, we still use scikit-image:
import napari
from skimage.io import imread
image = imread('../../data/Haase_MRT_tfl3d1.tif')
# print out the spatial dimensions of the image
print(image.shape)
(192, 256, 256)
# Create an empty viewer
viewer = napari.Viewer()
# Add a new layer containing an image
viewer.add_image(image)
<Image layer 'image' at 0x1d5a7bb9af0>
With this command, we can make a screenshot of napari and save it in our notebook.
napari.utils.nbscreenshot(viewer)
Slicing#
We can programmatically move the Z-slice slider like this.
dims = viewer.dims.current_step
dims
(96, 128, 128)
# modify Z-slice position
dims = list(dims)
dims[0] = 60
viewer.dims.current_step = dims
# take another screenshot
napari.utils.nbscreenshot(viewer)
3D rendering#
We can also activate the 3D view programmatically.
viewer.dims.ndisplay = 3
napari.utils.nbscreenshot(viewer)
Camera orientation#
For changing the camera position it is recommended to print out the camera, e.g. after a modification was done manually. This allows you to understand what center
, zoom
, angles
etc mean. You can then modify these parameters using Python.
viewer.camera
Camera(center=(95.5, 127.5, 127.5), zoom=2.2191406249999996, angles=(0.0, 0.0, 90.00000000000001), perspective=0.0, interactive=True)
viewer.camera.angles = (15, -15, 90)
napari.utils.nbscreenshot(viewer)