torchcvnn.transforms.PadIfNeeded

class torchcvnn.transforms.PadIfNeeded(min_height: int, min_width: int, border_mode: str = 'constant', pad_value: float = 0)[source]

Pad an image if its dimensions are smaller than specified minimum dimensions.

This transform pads images that are smaller than given minimum dimensions by adding padding according to the specified border mode. The padding is added symmetrically on both sides to reach the minimum dimensions when possible. If the minimum required dimension (height or width) is uneven, the right and the bottom sides will receive an extra padding of 1 compared to the left and the top sides.

Parameters:
  • min_height (int) – Minimum height requirement for the image

  • min_width (int) – Minimum width requirement for the image

  • border_mode (str) – Type of padding to apply (‘constant’, ‘reflect’, etc.). Default is ‘constant’.

  • pad_value (float) – Value for constant padding (if applicable). Default is 0.

Returns:

Padded image with dimensions at least min_height x min_width. Original image if no padding is required.

Return type:

np.ndarray | torch.Tensor

Example

>>> transform = PadIfNeeded(min_height=256, min_width=256)
>>> padded_image = transform(small_image)  # Pads if image is smaller than 256x256
__init__(min_height: int, min_width: int, border_mode: str = 'constant', pad_value: float = 0) None[source]

Methods

__init__(min_height, min_width[, ...])