Sunday, December 21, 2025
HomeLanguagesHow to Adjust Saturation of an image in PyTorch?

How to Adjust Saturation of an image in PyTorch?

In this article, we are going to discuss How to adjust the saturation of an image in PyTorch. 

adjust_saturation() method

Saturation is basically used to adjust the intensity of the color of the given Image, we can adjust the saturation of an image by using the adjust_saturation() method of torchvision.transforms module. The method accepts a batch of tensor images, PIL images, and tensor images as input. With tensor, we provide shapes in [C, H, W], where C represents the number of channels and  H, and W represents the height and width respectively. This method returns Saturation adjusted image.  The below syntax is used to adjust the saturation of an image.

Syntax: torchvision.transforms.functional.adjust_saturation(img, saturation_factor)

Parameter: 

  • img: This is our input image for which we adjust it’s saturation.
  • saturation_factor: This parameter is used to define how much saturation is to be adjusted. 0 and 1 will give a black-and-white and original image respectively. 

Return: This method returns a Saturation adjusted image.

The below image is used for demonstration:

 

Example 1

In this example, we are adjusting the saturation of an image.

Python3




# import required library
import torch
import torchvision
import torchvision.transforms as T
import torchvision.transforms.functional as F
from torchvision.io import read_image
  
# read the image from computer
pic = read_image('img.png')
  
# adjust the saturation of the input image
pic = F.adjust_saturation(pic, 10)
pic = T.ToPILImage()(pic)
  
# display result
pic.show()


Output:

How to Adjust Saturation of an image in PyTorch

 

Example 2

In this example, we are adjusting the saturation of an image when saturation_factor=0. it gives us a black and white image.

Python3




# import required library
import torch
import torchvision
import torchvision.transforms as T
import torchvision.transforms.functional as F
from torchvision.io import read_image
  
# read the image from computer
pic = read_image('img.png')
  
# adjust the saturation of the input image
# saturation_factor = 0
pic = F.adjust_saturation(pic, 0)
pic = T.ToPILImage()(pic)
  
# display result
pic.show()


Output:

How to Adjust Saturation of an image in PyTorch

 

Example 3

In this example, we are adjusting the saturation of an image when saturation_factor=1. it gives us the original image.

Python3




# import required library
import torch
import torchvision
import torchvision.transforms as T
import torchvision.transforms.functional as F
from torchvision.io import read_image
  
# read the image from computer
pic = read_image('img.png')
  
# adjust the saturation of the input image
# saturation_factor = 1
pic = F.adjust_saturation(pic, 1)
pic = T.ToPILImage()(pic)
  
# display result
pic.show()


Output:

How to Adjust Saturation of an image in PyTorch

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32455 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6911 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS