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:
- Upload the desired image to Google Colab
- Import the required libraries
- Read the input image
- Change the input image’s brightness
- Display the brightness-adjusted image
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:
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:
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”:
Step 5: Display the Brightness Adjusted Image
Finally, view the brightness-adjusted image by displaying it:
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:
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.