Napari scripts

To create and/or run Napari script, open a Jupyter notebook or Jupyter Lab from napari-env environment in Anaconda. Or, in case you have an open Napari viewer, you can use the script-editor:

Script-Editor

To get intellisense tips when writing the script one can write the code inside Napari (need to install the napari_script_editor plugin and then accessing it via the user interface Tools > Scripts > Script editor).

Recording

A simple way to get the syntax of running various commands from a script is to use the Script-Editor together with the Assistant (Plugins > Assistant, need to install the Napari-assistant plugin):

  1. In the Script-Editor check the Record checkbox

  2. In the Assistant navigate to the operation you want to perform

  3. run the operation > the underlying scripting is added to the Script-Editor

Open a new Napari viewer

import napari # napari package viewer = napari.Viewer() # cretae a new napari viewer

Add an image file to a napari viewer

Using napari.open command

there is more than one way to open a file. The simplest way, when no plugin is needed is by using napari.open command with parameter:

file_path = 'C:/Users/ehuds/Downloads/cropped napari 3d segmentation.tif' # change to your path viewer.open(file_path, name='my_first_image')

Using a specific plugin

When the file opening requires a specific plugin one can provide it using the plugin parameter. Imaris file, for example can be opened using the ‘bioformats’ plugin

file_path = 'C:/Users/ehuds/Downloads/yw1_2022-06-21_11.03.29_F02.ims' # change to your path viewer.open(file_path, plugin='bioformats', name='my_first_imaris_image')

Using skimage.io imread command

instead of a plugin one can try skimage.io to read the file

Toggle viewer between 2D and 3D view

3D images can be viewed in a 2D viewer as video (recommended when 3rd channel is time) or in 3D viewer (recommended when 3rd channel is Z-stack/slices). By default the viewer opens the image in 2D viewer. To switch to 3D

Segmentation

There are many plugins that can be used for segmentation.

napari-segment-blobs-and-things-with-membranes

This napari-plugin is based on scikit-image and allows segmenting nuclei and cells based on fluorescence microscopy images with high intensity in nuclei and/or membranes. It populates image processing operations to the Tools menu in napari. You can recognize them with their suffix (nsbatwm) in brackets. Furthermore, it can be used from the napari-assistant graphical user interface. Therefore, just click the menu Tools > Utilities > Assistant (na) or run naparia from the command line.

Here we show an example how to run this plugin from a script

segment the