- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 – Randomly change the brightness, contrast, saturation and hue of an image
To randomly change the brightness, contrast, saturation and hue of an image, we apply ColorJitter(). It's one of the transforms provided by the torchvision.transforms module. This module contains many important transformations that can be used to manipulate the image data.
ColorJitter() transformation accepts both PIL and tensor images. A tensor image is a PyTorch tensor with shape [C, H, W], where C is the number of channels, H is the image height, and W is the image width.
This transform also accepts a batch of tensor images. A batch of tensor images is a tensor with [B, C, H, W]. B is the number of images in the batch. If the image is neither a PIL image nor a tensor image, then we first convert it to a tensor image and then apply ColorJitter().
Syntax
torchvision.transforms.ColorJitter(brightness=0, contrast=0, saturation=0, hue=0)
It returns an image with different brightness, contrast, saturation and hue randomly chosen from the given respective range.
Steps
We could use the following steps to randomly change the brightness, contrast, saturation and hue of an image
Import the required libraries. In all the following Python 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 transforms from PIL import Image
Read the input image. The input image is a PIL image or a Torch tensor
img = Image.open('nike.jpg')
Define a transform to change brightness, contrast, saturation or hue. Give the desired rage value of these parameters.
transform = transforms.ColorJitter(brightness=(0.5,1.5),contrast=(1),saturation=(0.5,1.5),hue=(-0.1,0.1))
Apply the above defined transform on the input image to randomly change the brightness, contrast, saturation or hue.
img = transform(img)
Show the final output image.
img.show()
Note − In the following examples, you may get the output image with different brightness, contrast, saturation or hue because ColorJitter() transform randomly chooses these values from a given range. For example, for brightness = (0.5, 1.5), the brightness is any value in the range (0.5, 1.5).
Input Image
We will use the following image as the input in both the examples.
Example 1
Following is the Python3 program that randomly changes the brightness, contrast, saturation and hue of the original input image. In this example, we provide the values of the parameters in terms of range (min, max).
# import the required libraries import torch import torchvision.transforms as transforms from PIL import Image # Read the image img = Image.open('nike.jpg') # define a transform transform = transforms.ColorJitter(brightness=(0.5,1.5), contrast=(1), saturation=(0.5,1.5), hue=(-0.1,0.1)) # apply the above defined transform to randomly change # brightness, contrast, saturation and hue. img = transform(img) # visualize the image img.show()
Output
It will produce the following output −
Note that you may get the output image with a different brightness, contrast, saturation and hue.
Example 2
In this example, we provide the values of the parameters in terms of float values.
# import required libraries import torch import torchvision.transforms as transforms from PIL import Image # Read the image img = Image.open('nike.jpg') # define a transform transform = transforms.ColorJitter(brightness=1.0, contrast=0.5, saturation=1, hue=0.1) # apply above transform on input image img = transform(img) # visualize the image img.show()
Output
It will produce the following output −
Note that you may get the output image with a different brightness, contrast, saturation and hue.
- Related Articles
- How to change the contrast and brightness of an image using OpenCV in Python?
- Altering the brightness and contrast of an image using JavaFX and OpenCV
- How to set the brightness and contrast of an image with JavaScript?
- How to adjust saturation of an image in PyTorch?
- How to adjust the hue of an image in PyTorch?
- How to adjust the contrast of an image in PyTorch?
- How to adjust the brightness of an image in PyTorch?
- PyTorch – How to invert the colors of an image randomly with a given probability?
- How to change the brightness of an image in OpenCV using C++?
- Define colors using the Hue-Saturation-Lightness model (HSL) with CSS
- How to adjust the contrast of an image using contrast() function in Node Jimp?
- Define colors using the Hue-Saturation-Lightness-Alpha model (HSLA) with CSS
- How to spin the hue of an image using Jimp in NodeJS?
- JavaFX example to decrease the brightness of an image using OpenCV.
- How to alter the contrast of an image using Java OpenCV library?
