napari.Viewer#

class napari.Viewer(*, title='napari', ndisplay=2, order=(), axis_labels=(), show=True, camera: Camera = None, cursor: Cursor = None, dims: Dims = None, grid: GridCanvas = None, layers: LayerList = None, help: str = '', status: str | Dict = 'Ready', tooltip: Tooltip = None, theme: str = None, mouse_over_canvas: bool = False)[source]#

Bases: ViewerModel

Napari ndarray viewer.

Parameters:
  • title (string, optional) – The title of the viewer window. By default ‘napari’.

  • ndisplay ({2, 3}, optional) – Number of displayed dimensions. By default 2.

  • order (tuple of int, optional) – Order in which dimensions are displayed where the last two or last three dimensions correspond to row x column or plane x row x column if ndisplay is 2 or 3. By default None

  • axis_labels (list of str, optional) – Dimension names. By default they are labeled with sequential numbers

  • show – Whether to show the viewer after instantiation. By default True.

Methods

add_image([data, channel_axis, rgb, ...])

Add an image layer to the layer list.

add_labels(data, *[, affine, blending, ...])

Add a Labels layer to the layer list.

add_layer(layer)

Add a layer to the viewer.

add_points([data, ndim, features, ...])

Add a Points layer to the layer list.

add_shapes([data, ndim, features, ...])

Add a Shapes layer to the layer list.

add_surface(data, *[, features, ...])

Add a Surface layer to the layer list.

add_tracks(data, *[, features, properties, ...])

Add a Tracks layer to the layer list.

add_vectors([data, ndim, features, ...])

Add a Vectors layer to the layer list.

bind_key(key_bind[, func, overwrite])

Bind a key combination to a keymap.

close()

Close the viewer window.

close_all()

Class metod, Close all existing viewer instances.

construct([_fields_set])

Creates a new model setting __dict__ and __fields_set__ from trusted or pre-validated data.

copy(*[, include, exclude, update, deep])

Duplicate a model, optionally choose which fields to include, exclude and change.

dict(**kwargs)

Convert to a dictionary.

enums_as_values([as_values])

Temporarily override how enums are retrieved.

from_orm(obj)

json(**kwargs)

Serialize to json.

open(path, *[, stack, plugin, layer_type])

Open a path or list of paths with plugins, and add layers to viewer.

open_sample(plugin, sample[, reader_plugin])

Open sample from plugin and add it to the viewer.

parse_file(path, *[, content_type, ...])

parse_obj(obj)

parse_raw(b, *[, content_type, encoding, ...])

reset()

Reset the state of the model to default values.

reset_view()

Reset the camera view.

rounded_division(min_val, max_val, precision)

schema([by_alias, ref_template])

schema_json(*[, by_alias, ref_template])

screenshot([path, size, scale, canvas_only, ...])

Take currently displayed screen and convert to an image array.

show(*[, block])

Resize, show, and raise the viewer window.

update(values[, recurse])

Update a model in place.

update_console(variables)

Update console's namespace with desired variables.

update_forward_refs(**localns)

Try to update ForwardRefs on fields based on this Model, globalns and localns.

validate(value)

Attributes

axes

class_keymap

events

experimental

Experimental commands for IPython console.

keymap

scale_bar

text_overlay

window

Details

close()[source]#

Close the viewer window.

classmethod close_all() int[source]#

Class metod, Close all existing viewer instances.

This is mostly exposed to avoid leaking of viewers when running tests. As having many non-closed viewer can adversely affect performances.

It will return the number of viewer closed.

Returns:

number of viewer closed.

Return type:

int

screenshot(path=None, *, size=None, scale=None, canvas_only=True, flash: bool = True)[source]#

Take currently displayed screen and convert to an image array.

Parameters:
  • path (str) – Filename for saving screenshot image.

  • size (tuple (int, int)) – Size (resolution height x width) of the screenshot. By default, the currently displayed size. Only used if canvas_only is True.

  • scale (float) – Scale factor used to increase resolution of canvas for the screenshot. By default, the currently displayed resolution. Only used if canvas_only is True.

  • canvas_only (bool) – If True, screenshot shows only the image display canvas, and if False include the napari viewer frame in the screenshot, By default, True.

  • flash (bool) – Flag to indicate whether flash animation should be shown after the screenshot was captured. By default, True.

Returns:

image – Numpy array of type ubyte and shape (h, w, 4). Index [0, 0] is the upper-left corner of the rendered region.

Return type:

array

show(*, block=False)[source]#

Resize, show, and raise the viewer window.

update_console(variables)[source]#

Update console’s namespace with desired variables.

Parameters:

variables (dict, str or list/tuple of str) – The variables to inject into the console’s namespace. If a dict, a simple update is done. If a str, the string is assumed to have variable names separated by spaces. A list/tuple of str can also be used to give the variable names. If just the variable names are give (list/tuple/str) then the variable values looked up in the callers frame.