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('blobs.tif')

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

This little “magic” command is necessary before starting napari from notebooks.

%gui qt
import napari

# Create an empty viewer
viewer = napari.Viewer()
# Add a new layer containing an image
viewer.add_image(image)
<Image layer 'image' at 0x24299698b80>

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

napari.utils.nbscreenshot(viewer)
../_images/05_napari_7_0.png
# 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)
<Image layer 'image' at 0x2429b6598e0>

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)
<Image layer 'blurred_image' at 0x2429b7056a0>

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)
<Image layer 'background_subtracted_image' at 0x2429b6c31f0>

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

napari.utils.nbscreenshot(viewer)
../_images/05_napari_15_0.png

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?