Open In App

How to Adjust Saturation of an image in PyTorch?

Last Updated : 03 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads