Pads the `image` with the specified (height, width) `padding` and `mode`. Args: image (`np.ndarray`): The image to pad. padding (`int` or `Tuple[int, int]` or `Iterable[Tuple[int, int]]`): Padding to apply to the edges of the height, width axes. Can
(
image: np.ndarray,
padding: Union[int, Tuple[int, int], Iterable[Tuple[int, int]]],
mode: PaddingMode = PaddingMode.CONSTANT,
constant_values: Union[float, Iterable[float]] = 0.0,
data_format: Optional[Union[str, ChannelDimension]] = None,
input_data_format: Optional[Union[str, ChannelDimension]] = None,
)
| 665 | |
| 666 | |
| 667 | def pad( |
| 668 | image: np.ndarray, |
| 669 | padding: Union[int, Tuple[int, int], Iterable[Tuple[int, int]]], |
| 670 | mode: PaddingMode = PaddingMode.CONSTANT, |
| 671 | constant_values: Union[float, Iterable[float]] = 0.0, |
| 672 | data_format: Optional[Union[str, ChannelDimension]] = None, |
| 673 | input_data_format: Optional[Union[str, ChannelDimension]] = None, |
| 674 | ) -> np.ndarray: |
| 675 | """ |
| 676 | Pads the `image` with the specified (height, width) `padding` and `mode`. |
| 677 | |
| 678 | Args: |
| 679 | image (`np.ndarray`): |
| 680 | The image to pad. |
| 681 | padding (`int` or `Tuple[int, int]` or `Iterable[Tuple[int, int]]`): |
| 682 | Padding to apply to the edges of the height, width axes. Can be one of three formats: |
| 683 | - `((before_height, after_height), (before_width, after_width))` unique pad widths for each axis. |
| 684 | - `((before, after),)` yields same before and after pad for height and width. |
| 685 | - `(pad,)` or int is a shortcut for before = after = pad width for all axes. |
| 686 | mode (`PaddingMode`): |
| 687 | The padding mode to use. Can be one of: |
| 688 | - `"constant"`: pads with a constant value. |
| 689 | - `"reflect"`: pads with the reflection of the vector mirrored on the first and last values of the |
| 690 | vector along each axis. |
| 691 | - `"replicate"`: pads with the replication of the last value on the edge of the array along each axis. |
| 692 | - `"symmetric"`: pads with the reflection of the vector mirrored along the edge of the array. |
| 693 | constant_values (`float` or `Iterable[float]`, *optional*): |
| 694 | The value to use for the padding if `mode` is `"constant"`. |
| 695 | data_format (`str` or `ChannelDimension`, *optional*): |
| 696 | The channel dimension format for the output image. Can be one of: |
| 697 | - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format. |
| 698 | - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format. |
| 699 | If unset, will use same as the input image. |
| 700 | input_data_format (`str` or `ChannelDimension`, *optional*): |
| 701 | The channel dimension format for the input image. Can be one of: |
| 702 | - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format. |
| 703 | - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format. |
| 704 | If unset, will use the inferred format of the input image. |
| 705 | |
| 706 | Returns: |
| 707 | `np.ndarray`: The padded image. |
| 708 | |
| 709 | """ |
| 710 | if input_data_format is None: |
| 711 | input_data_format = infer_channel_dimension_format(image) |
| 712 | |
| 713 | def _expand_for_data_format(values): |
| 714 | """ |
| 715 | Convert values to be in the format expected by np.pad based on the data format. |
| 716 | """ |
| 717 | if isinstance(values, (int, float)): |
| 718 | values = ((values, values), (values, values)) |
| 719 | elif isinstance(values, tuple) and len(values) == 1: |
| 720 | values = ((values[0], values[0]), (values[0], values[0])) |
| 721 | elif isinstance(values, tuple) and len(values) == 2 and isinstance(values[0], int): |
| 722 | values = (values, values) |
| 723 | elif isinstance(values, tuple) and len(values) == 2 and isinstance(values[0], tuple): |
| 724 | values = values |