How to rotate an image by an angle using PyTorch in Python? Last Updated : 26 Apr, 2022 Comments Improve Suggest changes Like Article Like Report 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 the height and width of the image respectively. Syntax: torchvision.transforms.RandomRotation(Degrees, expand=False, center=None, fill=0, resample=None) Parameters: Degrees - The Range of degrees in which we want to rotate our image. expand - This is an optional Parameter. If it will true, expands the output to make it large enough to hold the entire rotated image and If it will false or omitted then make the output image the same size as the input image. Note that the expand flag assumes rotation around the center and no translation.center - This is also a optional parameter. it's center of rotation, (x, y). Origin is the upper left corner and the default is the center of the image.fill - Pixel fill value for the area outside the rotated image. Default is 0. If given a number then the value is used for all bands respectively.resample – This is also an optional parameter. This image is used as the input image in the following examples. Example 1: The following program is to rotate the image from the range of 60 to 90 degrees. Python3 # import required libraries import torch import torchvision.transforms as T from PIL import Image # read the image img = Image.open('GFG.jpg') # define a transform to rotate the image transform = T.RandomRotation(degrees=(60, 90)) # use above transform to rotate the image img = transform(img) # display result img.show() Output: Example 2: The following program is to rotate the image from the range of 30 to 45 degrees. Python3 # import required libraries import torch import torchvision.transforms as T from PIL import Image # read image img = Image.open('a.jpg') # define a transform transform = T.RandomRotation(degrees=(30, 45)) # use above transform to rotate the image img = transform(img) # display result img.show() Output: Comment More infoAdvertise with us Next Article How to rotate an image by an angle using PyTorch in Python? M mukulsomukesh Follow Improve Article Tags : Python Python-PyTorch Practice Tags : python Similar Reads How to rotate an image using Python? Image rotation in Python rotates an image around its centre by a specified angle using forward or inverse methods. When the angle isnât a multiple of 90 degrees, parts of the image may move outside the visible boundaries and get clipped. To avoid losing important content during rotation you need pro 3 min read How to crop an image at random location in PyTorch 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 a 2 min read 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 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 draw bounding boxes on an image in PyTorch? In this article, we are going to see how to draw bounding boxes on an image in PyTorch. draw_bounding_boxes() method The draw_bounding_boxes function helps us to draw bounding boxes on an image. With tensor we provide shapes in [C, H, W], where C represents the number of channels and  H, W represent 2 min read Like