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 napari 0.4.18 bundled app for a simple installation:
Linux:
napari-0.4.18-Linux-x86_64.sh
.macOS (Intel):
napari-0.4.18-macOS-x86_64.pkg
.macOS (Apple Silicon):
napari-0.4.18-macOS-arm64.pkg
.Windows:
napari-0.4.18-Windows-x86_64.exe
.Other artifacts can be found in the the Releases page, by the Assets section at the bottom.
For those familiar with Python:
napari can be installed on most macOS (Intel x86), Linux, and Windows systems with Python 3.8-3.10 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,
python -m 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: macOS users might need to right click on the app icon and select “Open” to bypass the security check. You can also go to System Settings > Privacy & Security and click on “Open Anyway”.
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
(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.
Add a new labels layer
Click on “paint”
Circle the cell
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.
Test some examples to see how to add different layer types and add your own widgets to napari.
Explore other plugins. A few fun ones: napari-clusters-plotter, napari-pyclesperanto-assistant, napari-animation.
If you don’t see the functions you need in existing plugins, try to make your own widget with magicgui.
If you’ve developed some functionality that you think would benefit the community as a whole, consider publishing it as a plugin!
Help the napari project by filing issues for bugs you encounter or features that you’d like to see! Or even better, come join the community meetings and get to know the team.