pad

paddle.vision.transforms. pad ( img, padding, fill=0, padding_mode='constant' ) [source]

Pads the given PIL.Image or numpy.array or paddle.Tensor on all sides with specified padding mode and fill value.

Parameters
  • img (PIL.Image|np.array|paddle.Tensor) – Image to be padded.

  • padding (int|list|tuple) – Padding on each border. If a single int is provided this is used to pad all borders. If list/tuple of length 2 is provided this is the padding on left/right and top/bottom respectively. If a list/tuple of length 4 is provided this is the padding for the left, top, right and bottom borders respectively.

  • fill (float, optional) – Pixel fill value for constant fill. If a tuple of length 3, it is used to fill R, G, B channels respectively. This value is only used when the padding_mode is constant. Default: 0.

  • padding_mode

    Type of padding. Should be: constant, edge, reflect or symmetric. Default: ‘constant’.

    • constant: pads with a constant value, this value is specified with fill

    • edge: pads with the last value on the edge of the image

    • reflect: pads with reflection of image (without repeating the last value on the edge)

      padding [1, 2, 3, 4] with 2 elements on both sides in reflect mode will result in [3, 2, 1, 2, 3, 4, 3, 2]

    • symmetric: pads with reflection of image (repeating the last value on the edge)

      padding [1, 2, 3, 4] with 2 elements on both sides in symmetric mode will result in [2, 1, 1, 2, 3, 4, 4, 3]

Returns

Padded image.

Return type

PIL.Image|np.array|paddle.Tensor

Examples

>>> import numpy as np
>>> from PIL import Image
>>> from paddle.vision.transforms import functional as F
>>> fake_img = (np.random.rand(256, 300, 3) * 255.).astype('uint8')
>>> fake_img = Image.fromarray(fake_img)
>>> padded_img = F.pad(fake_img, padding=1)
>>> print(padded_img.size)
(302, 258)

>>> padded_img = F.pad(fake_img, padding=(2, 1))
>>> print(padded_img.size)
(304, 258)