napari quick start

About napari

napari is a fast, interactive, multi-dimensional image viewer, with a vibrant plugin ecosystem that expands its capability to tackle various domain-specific visualization and analysis needs. It is built on Qt (for the GUI), vispy (for performant GPU-based rendering), and the scientific Python stack (numpy, scipy, and scikit-image).

napari is an open source project on GitHub to facilitate transparency, reuse, and extensibility.

At its core, it provides critical viewer features out-of-the-box, such as support for large multi-dimensional data; “layers” to simultaneously visualize images, models, and analysis results; and easy manual, interactive annotation in 3D.

This tutorial uses napari 0.4.14.

What’s covered here

This tutorial is for napari first-timers to give them a quick glance of what napari does, and give it a try right away. We will cover:

  • Installation

  • Open napari

  • Open an image

  • Image display adjustment

  • Manually label the cell

  • Get the cell area measurement

  • Next steps

Along the way, you will see how to access napari functions from Python code and from GUI - though for different purposes, one method might be easier than another. This quick start guide will not cover ALL possible methods but only some ways to perform basic tasks. For the more complete guide, please visit our usage guide.

You will also see some examples of plugins. The core napari viewer focuses on domain-agnostic functions such as layer controls. Analyses and domain specific functions, such as reading a special file format and image segmentation, live in the realm of plugins.

Installation

  • Download the bundled app for simple installation:

    Linux installation
    macOS installaion
    Windows installation

    Note: for the latest release, please visit here and look for Assets.

  • For those familiar with Python:

    napari can be installed on most macOS, Linux, and Windows systems with Python 3.7, 3.8, and 3.9 using pip.

    First, create a clean virtual environment:

conda create -y -n napari-env -c conda-forge python=3.9
conda activate napari-env
Once in napari-env,
pip install 'napari[all]'

If you run into any issues, please visit the more detailed installation guide, or report an issue on GitHub!

Open napari

napari can be opened in one of multiple ways, depending on how it’s used in your image analysis workflow.

Here we will be mainly focused on the GUI application.

  • From command line:

    Once installed, simply run

napari
  • If you installed the bundled app:

    Click on the app icon to open it.
    Note: for mac users, it may require security setting changes.

Open an image

napari natively supports tiff and many other formats supported by skimage.io.imread as input image file format.
Try with your own images or download this ome tiff file.

Additional input file formats may be supported by plugins. Try napari-aicsimageio if you have czi, lif, or nd2 files.

Once you have the proper plugin installed, use File > Open Files(s) and select the image file, or simply drag and drop the image into napari.

For demo purpose, we will use a sample image that comes with napari.

(1) Open napari IPython console

IPython console

(2) Type

from skimage import data
viewer.add_image(data.cell(), name='cell')

Image display adjustment

The layer controls panel at the upper left of the viewer allows you to adjust contrast, colormap, and other layer properties. These settings affect the visualization, but do not affect the underlying data.

To change the image display through the API, in IPython console, type

viewer.layers['cell'].colormap = "yellow"

Manually label the cell

To measure the area of the cell, we can use a labels layer and manually “paint” the cell. The labels layer allows you to record the segmentation result by assigning background = 0, and assigning each object with an integer.

  1. Add a new labels layer

  2. Click on “paint”

  3. Circle the cell

  4. Use “fill” bucket to fill it.

Several plugins can perform automatic segmentation that takes image layers as input and generates labels layers as output.

Try cellpose-napari if you have cell images.

Get the cell area measurement

To analyze labels our layer, we can use scikit-image, a popular Python library that comes with your napari installation. skimage.measure.regionprops provides a good set of features that can be extracted from labels, including area measurement.

In IPython console, type

from skimage.measure import regionprops
props = regionprops(viewer.layers['Labels'].data)
print("the cell area is: ", props[0].area)

Alternatively, try this plugin to have the result in a table form.

Note: the area reported by regionprops is the number of pixels. Check pixel size and convert the reported number to physical units.

Next steps

  • napari provides the flexibility to handle multi-dimensional data. Try opening 3D or higher dimensional images, and switch to 3D view.

ndisplay