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.
Example 1: Adjust Image Brightness with PIL
In the following example, we will brighten the image with a factor of 1, which gives our original image. Then with a factor of 1.5, which gives a brightened image. And then with a factor of 0.5, which gives a darkened image.
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') factor = 1.5 #brightens the image im_output = enhancer.enhance(factor) im_output.save('brightened-image.png')
Brightened Image – With increased brightness
Darkened Image – With decreased brightness
In this tutorial of Python Examples, we learned how to brighten an image, with the help of well detailed Python programs.