crop_and_pad_edges

crop_and_pad_edges(bounds, max_dimensions, region, pad_mode='constant', pad_constant_values=0)[source]

Apply padding to areas of a region which are outside max dimensions.

Applies padding to areas of the image region which have coordinates less than zero or above the width and height in max_dimensions. Note that bounds and max_dimensions must be given for the same image pyramid level (or more generally resolution e.g. if interpolated between levels or working in other units).

Note: This function is planned to be deprecated in the future when a transition from OpenSlide to tifffile as a dependency is complete. It is currently used to remove padding from OpenSlide regions before applying custom padding via numpy.pad(). This allows the behaviour when reading OpenSlide images to be consistent with other formats.

Parameters:
  • bounds (tuple(int)) – Bounds of the image region.

  • max_dimensions (tuple(int)) – The maximum valid x and y values of the bounds, i.e. the width and height of the slide.

  • region (numpy.ndarray) – The image region to be cropped and padded.

  • pad_mode (str) – The pad mode to use, see numpy.pad() for valid pad modes. Defaults to ‘constant’. If set to “none” or None no padding is applied.

  • pad_constant_values (int or tuple(int)) – Constant value(s) to use when padding. Only used with pad_mode constant.

Returns:

The cropped and padded image.

Return type:

numpy.ndarray

Examples

>>> from tiatoolbox.utils.image import crop_and_pad_edges
>>> import numpy as np
>>> region = np.ones((10, 10, 3))
>>> padded_region = crop_and_pad_edges(
...   bounds=(-1, -1, 5, 5),
...   max_dimensions=(10, 10),
...   region=image,
...   pad_mode="constant",
...   pad_constant_values=0,
... )