class mindspore.dataset.vision.c_transforms.RandomCrop(size, padding=None, pad_if_needed=False, fill_value=0, padding_mode=Border.CONSTANT)[source]

Crop the input image at a random location. If input image size is smaller than output size, input image will be padded before cropping.


If the input image is more than one, then make sure that the image size is the same.

  • size (Union[int, sequence]) – The output size of the cropped image. The size value(s) must be positive. If size is an integer, a square crop of size (size, size) is returned. If size is a sequence of length 2, it should be (height, width).

  • padding (Union[int, sequence], optional) – The number of pixels to pad the image The padding value(s) must be non-nagetive (default=None). If padding is not None, pad image first with padding values. If a single number is provided, pad all borders with this value. If a tuple or lists of 2 values are provided, pad the (left and top) with the first value and (right and bottom) with the second value. If 4 values are provided as a list or tuple, pad the left, top, right and bottom respectively.

  • pad_if_needed (bool, optional) – Pad the image if either side is smaller than the given output size (default=False).

  • fill_value (Union[int, tuple], optional) – The pixel intensity of the borders, only valid for padding_mode Border.CONSTANT. If it is a 3-tuple, it is used to fill R, G, B channels respectively. If it is an integer, it is used for all RGB channels. The fill_value values must be in range [0, 255] (default=0).

  • 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, means it fills the border with constant values.

    • Border.EDGE, means it pads with the last value on the edge.

    • Border.REFLECT, means it reflects the values on the edge omitting the last value of edge.

    • Border.SYMMETRIC, means it reflects the values on the edge repeating the last value of edge.

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

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

  • TypeError – If pad_if_needed is not of type boolean.

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

  • TypeError – If padding_mode is not of type Border.

  • ValueError – If size is not positive.

  • 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.vision import Border
>>> decode_op = c_vision.Decode()
>>> random_crop_op = c_vision.RandomCrop(512, [200, 200, 200, 200], padding_mode=Border.EDGE)
>>> transforms_list = [decode_op, random_crop_op]
>>> image_folder_dataset = image_folder_dataset.map(operations=transforms_list,
...                                                 input_columns=["image"])