Adjusting Image Brightness with Python and Pillow
If you have been following this newsletter, you will recall learning about changing an image's contrast and adjusting its color balance with Python. Today you will tackle the subject of image brightness!
Image brightness is a way to measure the intensity or luminosity of an image after it has been captured. Pillow’s ImageEnhance module allows you to modify the brightness of an image using the Brightness() class. If you use an enhancement factor of 0.0, Pillow will return a solid black image. When you use an enhancement factor of 1.0, you will get a copy of the original image returned.
For this example, you will be using a photo of Silver Falls from the state of Oregon:
To see how you can adjust the brightness of a photo with Python, open up your code editor and create a new file named enhance_brightness.py. Now add this code to the file:
In this code, you create an enhancer object using the Brightness() class. You use a factor of 1.5 for this example. When you run this code, you will see that the image has been brightened up considerably:
If you had used an enhancement factor of 2.5 as you did in the previous article on image adjustment, the image would have been very blown out. This one is brightened up more than necessary, but is still mostly acceptable. You should try a few different values on this photo or on one of your own.
Wrapping Up
Most cameras that you use today won’t need a lot of brightness adjustment unless you are taking pictures in a low-light location without a flash. Sometimes those photos can be saved by playing about with different brightness levels.
Occasionally you will need to adjust a photo’s brightness if the photo was taken with a bright background, such as a sunrise.
In either case, Python can help you improve your photos. This is especially true if you happened to take a bunch of photos at that same event and they all need to be tweaked slightly. Python is great for batch jobs such as this.
Give the Pillow package a try and see what you can do!
Want to Learn More?
You can learn more about what you can do with Python and Pillow in Mike’s book, Pillow: Image Processing with Python





