napari viewer tutorial¶
Welcome to the tutorial on the napari viewer!
This tutorial assumes you have already installed napari and know how to launch the viewer. For help with installation see our installation tutorial. For help launching the viewer see our getting started tutorial.
This tutorial will teach you about the napari viewer, including how to use its graphical user interface (GUI) and how the data within it is organized. At the end of the tutorial, you should understand both the layout of the viewer on the screen and the data inside of it.
Launching the viewer¶
As discussed in the getting started tutorial, the napari viewer can be launched from the command-line, a python script, an IPython console, or a Jupyter notebook. All four methods launch the same viewer, and anything related to interacting with the viewer on the screen applies equally to all of them. We will use the syntax for running the code inside a jupyter notebook with each code block below pasted into its own cell, but if you’d like to use a python script instead, simply copy and paste the code blocks into scripts with
napari.run() as the final line (this starts an event loop which will
open an interactive viewer) and run them.
Note: There is also an IPython console available in napari, when napari is launched from the terminal, from a Python script, or when you use the napari bundled app. You can open it with the IPython console button (far left viewer button) or with the menu option Window > console. You can use this console to programmatically interact with an open viewer using the API methods illustrated in this tutorial.
Let’s get started by launching a viewer with a simple 2D image.
The fastest way to open a viewer with an image on the canvas is using
from skimage import data import napari viewer, image_layer = napari.imshow(data.astronaut(), rgb=True)
imshow will return a
Viewer object that is the main object inside napari and a
Image layer object. All the data you add to napari will be stored inside the
Viewer object and will be accessible from it. This command will also open the viewer to create a GUI that you can interact with. The
Image will contain information about the image and allow you to access image methods.
You can also create an empty
Viewer directly and then start adding images to it. For example:
from skimage import data import napari viewer = napari.Viewer() new_layer = viewer.add_image(data.astronaut(), rgb=True)
After running either of those two commands, you should be able to see the photograph of the astronaut in the napari viewer as shown below:
Show code cell source Hide code cell source
from napari.utils import nbscreenshot nbscreenshot(viewer, alt_text="photograph of an astronaut in napari viewer")
Now we will continue exploring the rest of the viewer.
Layout of the viewer¶
The viewer is organized into a few key areas which are explained in the next sections:
Main Menu (top bar menu)
The image below has the areas of the viewer labeled:
We’ll go through each of these in the next sections.
The canvas is in the center of the viewer and contains the visual display of the data passed to napari, including
Shapes, and other supported data types. Under the hood, the canvas is a
vispy.scene.SceneCanvas object which has built-in support for features such as zooming and panning. As
OpenGL and your graphics card, panning and zooming are highly performant. You can return to the original zoom level by clicking the
home button in the viewer buttons panel.
Layers are one of the basic napari objects. There are different layer types for
Shapes, and other data types. They can be added to the viewer either programmatically or through the GUI. Once added, they populate the layer list located on the bottom left side of the canvas.
The layer list contains one widget for each of the layers that have been added to the viewer and includes a
thumbnail that shows a miniaturized version of the currently viewed data, a
name that is an editable text box, a
visibility button that can be toggled on or off to show or hide the layer, and an
icon for the layer type.
Adding the following three image layers using the code below adds three-layer widgets to the layer list as follows:
import napari from skimage import data viewer = napari.Viewer() viewer.add_image(data.astronaut(), name='astronaut') viewer.add_image(data.moon(), name='moon') viewer.add_image(data.camera(), name='camera')
Show code cell source Hide code cell source
nbscreenshot(viewer, alt_text="3 image layers shown in napari viewer with the canvas displaying a photograph of a man looking through a camcorder")