Edit2D#

class quantem.widget.Edit2D(**kwargs: Any)[source]#

Bases: AnyWidget

Interactive visual crop/pad tool for 2D images.

Display a 2D image with a draggable crop rectangle. The rectangle can be positioned anywhere – inside the image for cropping, extending beyond image bounds for padding, or fully enclosing the image for pure padding.

Parameters:
  • data (array_like) – 2D array (height, width) for a single image, or 3D array (N, height, width) or list of 2D arrays for multi-image mode. All images are cropped with the same region.

  • bounds (tuple of int, optional) – Initial crop bounds as (top, left, bottom, right) in image pixel coordinates. Negative values and values exceeding image dimensions are allowed for padding. If None, defaults to the full image extent.

  • fill_value (float, default 0.0) – Fill value for padded regions outside the original image bounds.

  • title (str, default "") – Title displayed in the widget header.

  • cmap (str, default "gray") – Colormap name.

  • pixel_size (float, default 0.0) – Pixel size in angstroms for scale bar display.

  • show_stats (bool, default True) – Show statistics bar.

  • show_controls (bool, default True) – Show control row.

  • show_display_controls (bool, default True) – Show display control group.

  • show_edit_controls (bool, default True) – Show edit control group.

  • show_histogram (bool, default True) – Show histogram control group.

  • log_scale (bool, default False) – Log intensity mapping.

  • auto_contrast (bool, default True) – Percentile-based contrast.

  • disabled_tools (list of str, optional) – Tool groups to disable in the frontend UI/interaction layer. Supported values: "mode", "edit", "display", "histogram", "stats", "navigation", "export", "view", "all".

  • disable_* (bool, optional) – Convenience flags (disable_mode, disable_edit, disable_display, disable_histogram, disable_stats, disable_navigation, disable_export, disable_view, disable_all) equivalent to including those tool names in disabled_tools.

  • hidden_tools (list of str, optional) – Tool groups to hide from the frontend UI. Hidden tools are also interaction-locked (equivalent to disabled for behavior).

  • hide_* (bool, optional) – Convenience flags (hide_mode, hide_edit, hide_display, hide_histogram, hide_stats, hide_navigation, hide_export, hide_view, hide_all) equivalent to including those tool names in hidden_tools.

Examples

>>> import numpy as np
>>> from quantem.widget import Edit2D
>>> img = np.random.rand(256, 256).astype(np.float32)
>>> crop = Edit2D(img)
>>> crop  # display, draw crop region interactively
>>> crop.result  # returns cropped NumPy array
>>> crop.crop_bounds  # (top, left, bottom, right) tuple

Key Methods

Edit2D.set_image(data, **kwargs)[source]#

Replace the image data.

Edit2D.save_image(path: str | Path, *, format: str | None = None, dpi: int = 150) Path[source]#

Save current image as PNG, PDF, or TIFF.

In crop mode, saves the cropped/padded result. In mask mode, saves the masked result.

Parameters:
  • path (str or pathlib.Path) – Output file path.

  • format (str, optional) – ‘png’, ‘pdf’, or ‘tiff’. If omitted, inferred from file extension.

  • dpi (int, default 150) – Output DPI metadata.

Returns:

The written file path.

Return type:

pathlib.Path

State Persistence

Edit2D.state_dict()[source]#
Edit2D.save(path: str)[source]#
Edit2D.load_state_dict(state)[source]#
Edit2D.summary()[source]#