pytorch

How to Adjust the Brightness of an Image in PyTorch?

Brightness refers to the overall intensity of light in a particular image. It specifies how light or dark the image appears. Users can adjust the brightness of any image to enhance its visibility and quality. PyTorch provides the “adjust_brightness()” method to adjust the brightness of a specific image. This method returns the brightness-adjusted image to users.

This blog will illustrate the method to change the brightness of an image in PyTorch.

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

To change the image’s brightness in PyTorch, look at the following steps:

Step 1: Upload an Image to Google Colab

First, open Google Colab and click on the below-highlighted icons. Then, choose the specific image from the computer and upload it:

Subsequently, the image will be uploaded to Google Colab:

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

Step 2: Import Necessary Library

Next, import the required libraries. For instance, we have imported the following libraries:

import torch

from PIL import Image

import torchvision.transforms.functional as F

Here:

  • import torch” imports the PyTorch library.
  • From PIL import Image” is used for opening and saving different image file formats.
  • import torchvision.transforms.functional as F” imports the functional module from “torchvision.transforms” that provides transformations:

Step 3: Read the Input Image

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

input_img = Image.open('nature_img.jpg')

Step 4: Change the Input Image’s Brightness

Now, adjust the brightness of the input image with the desired brightness factor using the “adjust_brightness()” method. Here, we are adjusting the brightness with a brightness factor “0.4”:

new_img = F.adjust_brightness(input_img, 0.4)

Step 5: Display the Brightness Adjusted Image

Finally, view the brightness-adjusted image by displaying it:

new_img

The above output shows that the brightness of the input image has been successfully adjusted with the specified brightness factor i.e., “0.4”.

Similarly, users can specify any other brightness factor to adjust the brightness of the image. Now, we will adjust the same image with the “1.7” brightness factor to see the difference:

new_img = F.adjust_brightness(input_img, 1.7)

This will increase the brightness of the image:

Note: The “0” value for the brightness factor gives a black image and “1” gives the original image. A value between 0 to 1 gives dark images while a value above 1 increases the brightness of the image.

Comparison

The comparison between the original image and brightness-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 brightness of an image in PyTorch.

Conclusion

To adjust/change the image’s brightness 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_brightness()” method to modify the input image’s brightness with the preferred brightness factor. Lastly, view the brightness-adjusted image by displaying it. This blog has illustrated the method to adjust/change the image’s brightness in PyTorch.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.