| by Arround The Web | No comments

How to Adjust the Saturation of an Image in PyTorch?

Saturation refers to the vibrancy and intensity of colors in the image. A high-saturation image has bright and vibrant colors, whereas a low-saturation image has dulled and faded colors. Users can adjust the saturation of any image to change its mood and appearance. PyTorch provides the “adjust_saturation()” method to change the saturation of the desired image. This method returns the saturation-adjusted image to users.

This article will illustrate the method to change the image’s saturation in PyTorch.

How to Adjust/Change the Image’s Saturation in PyTorch?

To adjust the image’s saturation in PyTorch, follow the below-mentioned steps:

Step 1: Upload the Desired Image to Google Colab

First, open Google Colab and upload the desired image from your PC by clicking on the below-highlighted icons:

By doing so the image will be uploaded:

Here, we have uploaded the following image and we will adjust the saturation of this image:

Step 2: Import Necessary Libraries

Then, import the necessary libraries. For instance, we have imported the following libraries:

import torch

import torchvision

import torchvision.transforms as T

import torchvision.transforms.functional as F

from torchvision.io import read_image

Here:

  • import torch” imports the PyTorch library.
  • import torchvision” imports the torchvision library that is used for tasks related to computer vision.
  • import torchvision.transforms as T” imports the transforms module from torchvision that is used to preprocess image data before feeding it into a neural network.
  • import torchvision.transforms.functional as F” imports the functional module from “torchvision.transforms” that provides transformations.
  • from torchvision.io import read_image” imports the “read_image” function from the “io” module in torchvision. It is used to read an image file from the file system and convert it into a PyTorch tensor:

Step 3: Read Input Image

Next, read the input image from the computer. Here, we are reading the “my_img.jpg” and storing it in the “input_img” variable:

input_img = read_image('my_img.jpg')

Step 4: Change the Input Image’s Saturation

Now, modify the saturation of the input image with the desired saturation factor using the “adjust_saturation()” method. Here, we are adjusting the saturation with a saturation factor “60”:

new_img = F.adjust_saturation(input_img, 60)

Step 5: Convert Adjusted Saturated Image to PIL Image

After that, use the following method to convert the adjusted saturated image to a PIL image:

new_img = T.ToPILImage()(new_img)

Step 6: Display the PIL Image

Finally, display the PIL image:

new_img

The above output shows that the saturation has been successfully applied on the input image with the specified saturation factor i.e., 60.

Similarly, users can specify any other saturation factor to adjust the image’s saturation. Now, we will adjust the same image with the “0” saturation factor to see the difference:

new_img = F.adjust_saturation(input_img, 0)

This has provided the black and white image:

Note: The “0” value for the saturation factor gives a black image and “1” gives the original image. A value above 1 increases the saturation of the image.

Comparison

The comparison between the original image and saturation-adjusted images can be seen below:

Note: You can access our Google Colab Notebook at this link.

We have efficiently explained the method of adjusting the saturation of the desired image in PyTorch.

Conclusion

To adjust/change the image’s saturation in PyTorch, first, upload the desired image to Google Colab. Then, import the required libraries and read the input image. After that, use the “adjust_saturation()” method to modify the input image’s saturation and convert it into the PIL image. Lastly, display the PIL image. This article has illustrated the method to adjust/change the image’s saturation in PyTorch.

Share Button

Source: linuxhint.com

Leave a Reply