RandomAffine() method
RandomAffine() method accepts PIL Image and Tensor Image. The tensor image is a PyTorch tensor with [C, H, W] shape, where C represents the number of channels and H, W represents the height and width respectively. This method returns the affine transformed image of the input image. The below syntax is used to perform the affine transformation of an image in PyTorch.
Syntax: torchvision.transforms.RandomAffine(degree)
Parameters:
- degree: This is our desired range of degree. It’s a sequence like (min, max).
Return: This method returns the affine transformed image of the input image.
The below image is used for demonstration:
Example 1:
The following example is to understand how to perform the random affine transformation of an image in PyTorch whereas, the desired range of degree is (50,60).
Python3
# import required libraries import torch from PIL import Image import torchvision.transforms as transforms # Read input image from computer img = Image. open ( 'a.jpg' ) # define an transform transform = transforms.RandomAffine(( 50 , 60 )) # apply the above transform on image img = transform(img) # display image after apply transform img.show() |
Output:
Example 2:
The following example is to know how to perform the random affine transformation of an image in PyTorch whereas, the desired range of degrees is (30).
Python3
# import required libraries import torch from PIL import Image import torchvision.transforms as transforms # Read input image from computer img = Image. open ( 'a.jpg' ) # define an transform transform = transforms.RandomAffine(( 30 )) # apply the above transform on image img = transform(img) # display image after apply transform img.show() |
Output:
How to perform random affine transformation of an image in PyTorch
In this article, we will cover how to perform the random affine transformation of an image in PyTorch.