Adjust Image Brightness using Pillow - Python Examples
Python - Adjust Image Brightness using Pillow Library
You can adjust the brightness of an image using Python Pillow library. Adjusting the brightness mean, either increasing the pixel value evenly across all channels for the entire image to increase the brightness, or decreasing the pixel value evenly across all channels for the entire image to decrease the brightness.
Increasing the brightness makes image whiter. And decreasing brightness darkens the image.
Steps to Adjust Image Brightness using PIL
To adjust image brightness using Python Pillow,
- Read the image using Image.open().
- Create ImageEnhance.Brightness() enhancer for the image.
- Enhance the image brightness using enhance() method, by the required factor.
By adjusting the factor you can brighten or dull the image.
While a factor of 1 gives original image. Making the factor towards 0 makes the image black, while factor>1 brightens the image.
Examples
1. Increate given image brightness
In the following example, we will brighten the image by a factor of 1.5, which gives a brightened image.
Python Program
from PIL import Image, ImageEnhance
#read the image
im = Image.open("sample-image.png")
#image brightness enhancer
enhancer = ImageEnhance.Brightness(im)
factor = 1 #gives original image
im_output = enhancer.enhance(factor)
im_output.save('original-image.png')
factor = 1.5 #brightens the image
im_output = enhancer.enhance(factor)
im_output.save('brightened-image.png')
Original Image
Brightened Image - With increased brightness
2. Decrease the brightness of given image
In the following example, we will decrease the brighten the image by a factor of 0.5, which gives a darkened image.
Python Program
from PIL import Image, ImageEnhance
#read the image
im = Image.open("sample-image.png")
#image brightness enhancer
enhancer = ImageEnhance.Brightness(im)
factor = 1 #gives original image
im_output = enhancer.enhance(factor)
im_output.save('original-image.png')
factor = 0.5 #darkens the image
im_output = enhancer.enhance(factor)
im_output.save('darkened-image.png')
Original Image
Darkened Image - With decreased brightness
Summary
In this tutorial of Python Examples, we learned how to brighten an image, with the help of well detailed Python programs.