mindspore.dataset.vision.PadToSize

class mindspore.dataset.vision.PadToSize(size, offset=None, fill_value=0, padding_mode=Border.CONSTANT)[source]

Pad the image to a fixed size.

Parameters
  • size (Union[int, Sequence[int, int]]) – The target size to pad. If int is provided, pad the image to [size, size]. If Sequence[int, int] is provided, it should be in order of [height, width].

  • offset (Union[int, Sequence[int, int]], optional) – The lengths to pad on the top and left. If int is provided, pad both top and left borders with this value. If Sequence[int, int] is provided, is should be in order of [top, left]. Default: None, means to pad symmetrically, keeping the original image in center.

  • fill_value (Union[int, tuple[int, int, int]], optional) – Pixel value used to pad the borders, only valid when padding_mode is Border.CONSTANT. If int is provided, it will be used for all RGB channels. If tuple[int, int, int] is provided, it will be used for R, G, B channels respectively. Default: 0.

  • padding_mode (Border, optional) –

    Method of padding. It can be Border.CONSTANT, Border.EDGE, Border.REFLECT or Border.SYMMETRIC. Default: Border.CONSTANT.

    • 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.

Raises
Supported Platforms:

CPU

Examples

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>>
>>> # Use the transform in dataset pipeline mode
>>> data = np.random.randint(0, 255, size=(1, 100, 100, 3)).astype(np.uint8)
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> transforms_list = [vision.PadToSize([256, 256])]
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list, input_columns=["image"])
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(256, 256, 3) uint8
>>>
>>> # Use the transform in eager mode
>>> data = np.random.randint(0, 255, size=(100, 100, 3)).astype(np.uint8)
>>> output = vision.PadToSize([256, 256])(data)
>>> print(output.shape, output.dtype)
(256, 256, 3) uint8
Tutorial Examples: