Python: Getting started with napari

Visualizing and processing microscopy images using Python

Posted on: 17 May 2025

I have been playing around with napari, an open-source multidimensional image viewer for Python. It is quite useful for visualizing microscopy images, particularly when dealing with multiple channels and Z-stacks.

One of the nice things about napari is that you can directly interact with the viewer through Python. So, you can do all your image processing in Python and visualize the results as you go along. There are also quite a few plugins available for doing different image analysis tasks.

Here are some notes on getting started with napari, mainly for my own reference.

Installation

I use Miniforge for managing my Python environments. It is probably a good idea to create a separate environment for napari, especially if you are going to play around with different plugins.

Open the Miniforge prompt, navigate to your working directory, and create a new environment.

conda create -y -n ni-napari-env -c conda-forge python=3.13
conda activate ni-napari-env

Install napari using pip.

python -m pip install "napari[all]"

To check if everything works, simply type:

napari

This should open the napari viewer.

Getting started

There are two ways I generally use napari.

Option 1: From the terminal

Open the Miniforge prompt, activate the environment, and launch napari.

conda activate ni-napari-env
napari

You can now simply drag and drop your images into the viewer.

Option 2: From a Jupyter notebook in VS Code

I prefer this method since I can do the image processing and visualization from the same notebook.

Make sure you have the necessary packages installed in your environment.

python -m pip install ipykernel scikit-image

Open VS Code, create a new Jupyter notebook, and select ni-napari-env as the Python kernel.

Let’s try opening one of the example images from scikit-image.

from skimage import data
import napari

viewer, image_layer = napari.imshow(
    data.astronaut(), rgb=True
)

This should open a napari window with an image of an astronaut. You can now interact with the viewer directly from the notebook.

Working with images and layers

The nice thing about napari is that everything is organized into layers, somewhat similar to how things work in Photoshop or Inkscape. You can have different images, masks, points, and shapes as separate layers.

First, create an empty viewer.

import napari
import numpy as np

viewer = napari.Viewer()

If you are doing this from a standalone Python script rather than a Jupyter notebook, you would also need to call napari.run() at the end of the script.

You can now drag and drop your .tif images into the viewer.

Inspecting the layers

To see what layers are currently present in the viewer:

viewer.layers

To get the number of layers:

len(viewer.layers)

If you are curious about all the properties of a particular layer, you can inspect them using the following code.

print("Layer Properties and Values:")

for prop in dir(viewer.layers[0]):
    if not prop.startswith('_'):
        value = getattr(viewer.layers[0], prop)
        print(f"{prop}: {value}")

There are quite a lot of things you can access directly from Python!

Getting the image data

You can access the actual image data from a layer and check its dimensions.

imagedata = viewer.layers[0].data

print(f"Image data shape: {imagedata.shape}")

This is particularly useful when working with multidimensional microscopy images. It is always a good idea to check the dimensions of the image before doing any processing.

Splitting the channels

Suppose we have a two-channel Z-stack with dimensions in the order (Z, C, Y, X).

We can split the channels using simple NumPy indexing.

layer_1_data = imagedata[:, 0, :, :]
layer_2_data = imagedata[:, 1, :, :]

And add them back to the viewer as separate layers.

viewer.add_image(
    layer_1_data,
    name='Channel 1',
    colormap='green'
)

viewer.add_image(
    layer_2_data,
    name='Channel 2',
    colormap='magenta'
)

You could also do this directly using napari.

viewer.add_image(imagedata, channel_axis=1)

Of course, the axis along which you split the image depends on how your image data is organized.

Basic image processing with Python and napari

I came across this really nice resource called BioImage Analysis Notebooks. There is almost everything you need to get started with bioimage analysis in Python, along with examples.

Of course, you can always ask the robots these days. But it is quite useful to have a collection of working examples and explanations of the different image processing methods.

The examples below follow the BioImage Analysis Notebooks, which go into much more detail, so head there if you want to learn properly. I am just keeping a few simple ones here, using scikit-image and napari, for my own reference.

Loading an image

from skimage.io import imread

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

print(image.shape)

The blobs.tif image is in the data folder of the BioImage Analysis Notebooks repository. You can also use any 2D image of your own; just change the path.

Create a viewer and add the image as a new layer.

import napari

viewer = napari.Viewer()
viewer.add_image(image, name='Original');

You can also get a screenshot of the viewer directly within the Jupyter notebook.

napari.utils.nbscreenshot(viewer)

Quite useful when documenting your image analysis in a notebook.

Changing the lookup table (LUT)

Let’s add the same image again, but with a different colormap.

viewer.add_image(
    image,
    name='Green LUT',
    colormap='green'
);

This only changes how the image is displayed, not the actual pixel values.

Gaussian blur

We can apply a Gaussian filter to the image using scikit-image and add the resulting image as another layer.

from skimage.filters import gaussian

blurred_image = gaussian(
    image,
    sigma=5,
    preserve_range=True
)

viewer.add_image(
    blurred_image,
    name='Gaussian blur'
);

Background subtraction

One way to remove a slowly varying background from a 2D image containing bright objects is to use a white top-hat filter.

from skimage.morphology import white_tophat, disk

background_subtracted_image = white_tophat(
    image,
    disk(25)
)

viewer.add_image(
    background_subtracted_image,
    name='Background subtracted'
);

The size of the disk can be adjusted depending on the size of the structures in the image.

What I particularly like about this workflow is that you can keep adding the processed images as separate layers and compare them with the original image. No need to keep saving intermediate images and opening them in another software.

Some useful napari plugins

There are quite a few plugins available for napari. Here are a couple that I have been playing around with, particularly for segmentation of the lumen in microscopy Z-stacks. You can install them with pip in the same environment, or from the Plugins menu in napari.

python -m pip install napari-label-interpolator napari-convpaint

napari-label-interpolator

GitHub repository

This plugin allows you to interpolate labels along a particular dimension of a multidimensional image.

The idea is quite simple. Instead of manually segmenting every slice of a Z-stack, you can manually label a few slices and let the plugin interpolate the masks in between.

The general procedure is:

I found this method to be quite useful for lumen segmentation. You can save quite a bit of time compared to manually drawing the masks for every single slice. Of course, you still need to check the interpolated masks.

napari-convpaint

Installation and usage instructions

This is another interesting plugin for pixel classification. You can annotate a few regions belonging to different classes and train a classifier to predict the classes for the remaining pixels.

However, for the lumen segmentation I was trying to do, I found the label interpolation approach to be more useful than ConvPaint.

Useful resources

There is obviously a lot more one can do with napari. But I find the combination of Python, Jupyter notebooks, scikit-image, and napari quite convenient for experimenting with different image processing methods.