class mindspore.dataset.vision.py_transforms.Pad(padding, fill_value=0, padding_mode=Border.CONSTANT)[source]

Pad the input image on all sides with the given padding parameters.

  • padding (Union[int, sequence]) – The number of pixels padded on the image borders. If a single number is provided, pad all borders with this value. If a sequence of length 2 is provided, pad the left and top with the first value and the right and bottom with the second value. If a sequence of length 4 is provided, pad the left, top, right and bottom respectively.

  • fill_value (Union[int, tuple], optional) – Pixel fill value to pad the borders, only valid when padding_mode is Border.CONSTANT (default=0). If fill_value is an integer, it is used for all RGB channels. If fill_value is a tuple of length 3, it is used to fill R, G, B channels respectively.

  • padding_mode (Border, optional) –

    The method of padding (default=Border.CONSTANT). It can be any of [Border.CONSTANT, Border.EDGE, Border.REFLECT, Border.SYMMETRIC].

    • Border.CONSTANT, pads with a constant value.

    • Border.EDGE, pads with the last value at the edge of the image.

    • Border.REFLECT, pads with reflection of the image omitting the last value on the edge.

    • Border.SYMMETRIC, pads with reflection of the image repeating the last value on the edge.

  • TypeError – If padding is not of type integer or sequence of integer.

  • TypeError – If fill_value is not of type integer or tuple of integer.

  • TypeError – If padding_mode is not of type Border.

  • ValueError – If padding is negative.

  • ValueError – If fill_value is not in range [0, 255].

  • RuntimeError – If given tensor shape is not <H, W> or <H, W, C>.

Supported Platforms:



>>> from mindspore.dataset.transforms.py_transforms import Compose
>>> transforms_list = Compose([py_vision.Decode(),
...                            # adds 10 pixels (default black) to each border of the image
...                            py_vision.Pad(padding=10),
...                            py_vision.ToTensor()])
>>> # apply the transform to dataset through map function
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns="image")