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.

Updated on: 06-Jan-2022

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements