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:

from skimage.io import imread

image = imread('../../data/blobs.tif')

# print out the spatial dimensions of the image
print(image.shape)
(254, 256)
import napari

# Create an empty viewer
viewer = napari.Viewer()
# Add a new layer containing an image
viewer.add_image(image);

With this command, we can make a screenshot of napari and save it in our notebook.

napari.utils.nbscreenshot(viewer)
# Remove all layers to start from scratch
for l in viewer.layers:
    viewer.layers.remove(l)
# add the image again with a different lookup table
viewer.add_image(image, colormap='green');

We now blur the image and put it in the viewer

from skimage.filters import gaussian
blurred_image = gaussian(image, sigma=5)

# Add to napari
viewer.add_image(blurred_image);

We now apply background subtraction to the image and add it to the viewer

from skimage.morphology import white_tophat, disk
background_subtracted_image = white_tophat(image, disk(25))

# Add a new layer containing an image
viewer.add_image(background_subtracted_image);

By clicking the galery button (bottom left), we can view the different images side by side:

napari.utils.nbscreenshot(viewer)

Exercise#

Start a new notebook, import napari, load the image ../data/hela-cells.tif and add its three channels independently to napari as three layers. Afterwards, play with colormap and blending in the user interface. Can you make it look similar to ImageJ? Also check out the napari image layer tutorial. Can you also code this?