class paddle.vision.transforms. RandomCrop ( size, padding=None, pad_if_needed=False, fill=0, padding_mode='constant', keys=None ) [source]

Crops the given CV Image at a random location.

  • size (sequence|int) – Desired output size of the crop. If size is an int instead of sequence like (h, w), a square crop (size, size) is made.

  • padding (int|sequence|optional) – Optional padding on each border of the image. If a sequence of length 4 is provided, it is used to pad left, top, right, bottom borders respectively. Default: 0.

  • pad_if_needed (boolean|optional) – It will pad the image if smaller than the desired size to avoid raising an exception. Default: False.

  • keys (list[str]|tuple[str], optional) – Same as BaseTransform. Default: None.

  • img(PIL.Image|np.ndarray|Paddle.Tensor): The input image with shape (H x W x C).

  • output(PIL.Image|np.ndarray|Paddle.Tensor): A random cropped image.


A callable object of RandomCrop.


import numpy as np
from PIL import Image
from paddle.vision.transforms import RandomCrop

transform = RandomCrop(224)

fake_img = Image.fromarray((np.random.rand(324, 300, 3) * 255.).astype(np.uint8))

fake_img = transform(fake_img)