phenotypic.correction.ImageCropper#

class phenotypic.correction.ImageCropper(left: int | None = None, right: int | None = None, top: int | None = None, bottom: int | None = None)[source]

Bases: ImageCorrector

Remove pixels from image edges by specifying crop margins.

Crops all image components (rgb, gray, detect_mat, objmask, objmap) together. When applied to a GridImage, the grid structure is preserved and grid positions are recalculated for the cropped dimensions.

Parameters:
  • left (int | None) – Pixels to remove from the left edge. None means no cropping. Default: None.

  • right (int | None) – Pixels to remove from the right edge. Default: None.

  • top (int | None) – Pixels to remove from the top edge. Default: None.

  • bottom (int | None) – Pixels to remove from the bottom edge. Default: None.

Returns:

Input image with all components cropped to the specified margins.

Return type:

Image

Best For:
  • Removing scanner margins or borders outside the agar plate.

  • Eliminating edge artifacts (bent agar, labeling, moisture).

  • Standardizing image dimensions across a batch of plates.

Consider Also:
  • ImagePadder for adding pixels instead of removing them.

  • BorderObjectRemover for removing edge-touching colonies without changing image dimensions.

See also

How To: Crop and Pad Images for Batch Consistency for a visual walkthrough of cropping and padding operations.

Methods

__init__

Initialize an ImageCropper with pixel margins to remove from each edge.

apply

Applies the operation to an image, either in-place or on a copy.

widget

Return (and optionally display) the root widget.

__init__(left: int | None = None, right: int | None = None, top: int | None = None, bottom: int | None = None)[source]

Initialize an ImageCropper with pixel margins to remove from each edge.

Creates a cropper that removes the specified number of pixels from each edge of the image. All parameters are optional and default to None (no cropping from that edge).

Parameters:
  • left (int | None, optional) – Number of pixels to remove from the left edge. Must be non-negative. If None, the left edge is not cropped (equivalent to 0). Defaults to None.

  • right (int | None, optional) – Number of pixels to remove from the right edge. Must be non-negative. If None, the right edge is not cropped (equivalent to 0). Defaults to None.

  • top (int | None, optional) – Number of pixels to remove from the top edge. Must be non-negative. If None, the top edge is not cropped (equivalent to 0). Defaults to None.

  • bottom (int | None, optional) – Number of pixels to remove from the bottom edge. Must be non-negative. If None, the bottom edge is not cropped (equivalent to 0). Defaults to None.

Raises:

ValueError – If any parameter is negative. All crop margins must be non-negative integers (or None).

__del__()

Automatically stop tracemalloc when the object is deleted.

__getstate__()

Prepare the object for pickling by disposing of any widgets.

This ensures that UI components (which may contain unpickleable objects like input functions or thread locks) are cleaned up before serialization.

Note

This method modifies the object state by calling dispose_widgets(). Any active widgets will be detached from the object.

apply(image: Image, inplace: bool = False) Image

Applies the operation to an image, either in-place or on a copy.

Parameters:
  • image (Image) – The arr image to apply the operation on.

  • inplace (bool) – If True, modifies the image in place; otherwise, operates on a copy of the image.

Returns:

The modified image after applying the operation.

Return type:

Image

widget(image: Image | None = None, show: bool = False) Widget

Return (and optionally display) the root widget.

Parameters:
  • image (Image | None) – Optional image to visualize. If provided, visualization controls will be added to the widget.

  • show (bool) – Whether to display the widget immediately. Defaults to False.

Returns:

The root widget.

Return type:

ipywidgets.Widget

Raises:

ImportError – If ipywidgets or IPython are not installed.