How to crop an image at random location in PyTorch Last Updated : 22 Apr, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss how to pad an image on all sides in PyTorch. Torchvision.transforms.RandomCrop method Cropping is a technique of removal of unwanted outer areas from an image to achieve this we use a method in python that is torchvision.transforms.RandomCrop(). It is used to crop an image at a random location in PyTorch. This method accepts images like PIL Image and Tensor Image. The tensor image is a PyTorch tensor with [C, H, W] shape, where C represents a number of channels and H, W represents height and width respectively. Syntax: torchvision.transforms.RandomCrop(size) Parameters: size: Desired crop size of the image. Return: it returns the cropped image of given input size. Image used for demonstration: Example 1: In this example, we are transforming the image with a height of 200 and a width of 400. Python3 # import required libraries import torch import torchvision.transforms as transforms from PIL import Image # Read image image = Image.open('pic.jpg') # create an transform for crop the image # 200px height and 400px wide transform = transforms.RandomCrop((200, 400)) # use above created transform to crop # the image image_crop = transform(image) # display result image_crop.show() Output: Example 2: In this example, we are transforming the image at the center. In this, we will get a square image as output. Python3 # import required libraries import torch import torchvision.transforms as transforms from PIL import Image # Read image image = Image.open('a.jpg') # create an transform for crop the image transform = transforms.RandomCrop(300) # use above created transform to crop # the image image_crop = transform(image) # display result image_crop.show() Output: Comment More infoAdvertise with us Next Article How to crop an image at random location in PyTorch M mukulsomukesh Follow Improve Article Tags : Python Python-PyTorch Practice Tags : python Similar Reads How to crop an image at center in PyTorch? In this article, we will discuss how to crop an image at the center in PyTorch. CenterCrop() method We can crop an image in PyTorch by using the CenterCrop() method. This method accepts images like PIL Image, Tensor Image, and a batch of Tensor images. The tensor image is a PyTorch tensor with [C, 2 min read How to Read a JPEG or PNG Image in PyTorch In this article, we are going to discuss how to Read a JPEG or PNG Image using PyTorch in Python. image_read() method In PyTorch, the image_read() method is used to read an image as input and return a tensor of size [C, H, W], where C represents a number of channels and H, W represents height and wi 2 min read How to pad an image on all sides in PyTorch? In this article, we will discuss how to pad an image on all sides in PyTorch. transforms.pad() method Paddings are used to create some space around the image, inside any defined border. We can set different paddings for individual sides like (top, right, bottom, left). transforms.Pad() method is us 2 min read How to rotate an image by an angle using PyTorch in Python? In this article, we are going to see how to rotate an image by an angle in PyTorch. To achieve this, we can use RandomRotation() method. RandomRotation() transform accepts both PIL and tensor images. A Tensor Image is a tensor with (C, H, W) shape, C is for the number of channels, H and W are for th 2 min read How to Make a grid of Images in PyTorch? In this article, we are going to see How to Make a grid of Images in PyTorch. we can make a grid of images using the make_grid() function of torchvision.utils package. make_grid() function: The make_grid() function accept 4D tensor with [B, C ,H ,W] shape. where B represents the batch size, C repres 3 min read Like