- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PyTorch – How to rotate an image by an angle?
RandomRotation() rotates an image by a random angle. The chosen random angle is from a given range of angles in degree. RandomRotation() is one of the many important transforms provided by the torchvision.transforms module. RandomRotation() transform accepts both PIL and tensor images.
A tensor image is a Torch tensor with shape [C, H, W], where C is the number of channels, H is the image height, and W is the image width. If the image is neither a PIL image nor a tensor image, then we first convert it to a tensor image and then apply the transform.
Where degree is the desired range of degree. It's a sequence like (min, max). The image is rotated with a random angle selected from this range.
We could use the following steps to rotate an image with a random angle −
Import the required libraries. In all the following examples, the required Python libraries are torch, Pillow, and torchvision. Make sure you have already installed them.
import torch import torchvision import torchvision.transforms as T from PIL import Image
Read the input image. The input image is a PIL image or a torch tensor
img = Image.open('desert.jpg')
Define a transform to rotate an image with a random angle. Give the desired range of degrees.
transform = T.RandomRotation((30,70))
Apply the above-defined transform on the input image to rotate the input image with a random angle.
rotated_img = transform(img)
Show the output image.
This image is used as the input file in all the following examples.
In this example, we set the range of angles to (30,60). The angle by which the input image is rotated is randomly picked from this range.
# import required libraries import torch import torchvision.transforms as T from PIL import Image # read the input image img = Image.open('desert.jpg') # define a transform to rotate he input image transform = T.RandomRotation(degrees=(30,60)) # rotate the input image using above defined trasnform img = transform(img) # dispaly the rotated image img.show()
It will produce the following output −
Notice that the input image is rotated by an angle from the range (30,60).
In this example, we will demonstrate how you can rotate an input image by different angles.
import torch import torchvision.transforms as T from PIL import Image import matplotlib.pyplot as plt # read the input image img = Image.open('desert.jpg') # define a transform to rotate the image transform = T.RandomRotation(degrees = (0,180)) # save four output images applying the above transform imgs = [transform(img) for _ in range(4)] fig = plt.figure(figsize=(7,4)) rows, cols = 2,2 for j in range(0, len(imgs)): fig.add_subplot(rows, cols, j+1) plt.imshow(imgs[j]) plt.xticks() plt.yticks() plt.show()
It will produce the following output −
Notice that all four output images are rotated by different angles. The angle range is set to (0,180). Every image is rotated by an angle within the range (0,180).
- How to rotate an Image in ImageView by an angle on Android?
- How to Rotate image in image view by an angle in Android?
- How to rotate an image in imageview by an angle on Android using Kotlin?
- How to rotate an image in imageview by an angle on iOS App using Swift?
- How to rotate an image with a given angle using imagerotate() function in PHP?
- How to rotate an image with the canvas HTML5 element from the bottom center angle?
- How to rotate an image in Node Jimp?
- Rotate the element based on an angle using CSS
- How to rotate an image with OpenCV using Java?
- How to rotate an image in OpenCV using C++?
- How to convert an image to a PyTorch Tensor?
- PyTorch – How to convert an image to grayscale?
- How to crop an image at center in PyTorch?
- How to adjust saturation of an image in PyTorch?
- How to pad an image on all sides in PyTorch?