Adjusting Image Color Balance with Python
Pillow includes the ImageEnhance A module that contains some classes you can use to enhance your images. All of the included classes implement a common interface via the enhance() method. This makes learning how to use ImageEnhance easier.
You can read more about these classes in the documentation.
In this article, you will focus only on adjusting an image’s color balance using Python and Pillow.
Let’s get started!
Adjust Color Balance
Pillow allows you to modify an image’s color balance. You can do something similar on most televisions and computer monitors. You use a floating point number that Pillow calls a factor, which it then uses to change the color balance. If you set the factor to 0.0, the image will be in black and white. A factor of 1.0 returns a copy of the original image.
Factors lower than 1.0 will result in less color in the image. Factors greater than 1.0 will enhance the color. If you increase the factor too much, the colors will be too bright. There really are no bounds for the factor, but in most cases, you won’t want to go higher than 3 or 4.
For this example, you will be using this image of a Goldenrod Soldier Beetle:
Go ahead and create a new file and name it enhance_color.py, then enter the following code in your Python editor:
When you run this code, you create an instance of ImageEnhance.Color(). Then you call its enhance() method using the passed-in factor amount. In this example, you use 0.0, which will convert the image to black and white.
Here is the result:
Now go back to your code and change the factor to 0.5 instead of 0.0. Then re-run the code. Now your image will look like this:
See how washed out the image is now? It’s no long black and white, but doesn’t have the vibrant color of the original either.
Now try changing the factor to 2.5. This should push the color balance a bit out of whack. When you run the code this time, you will end up with the following image:
The beetle is now mostly orange rather than yellow. The other colors in the photo have changed quite a bit, too. Here is the original image next to the enhanced image so you can see the differences more easily:
You should try using different factor amounts with this image or with one of your own. You can significantly change how your photo looks using this method.
Wrapping Up
Adjusting an image’s color with Python and Pillow is easy. Of course, Pillow allows you to do much more than simply adjust the color of an image. You can change an image’s contrast, brightness, sharpness, and much more as well!
However, learning the basics is always a good place to start and you don’t usually want to overprocess an image anyway. Give Pillow a try and see what you can do with one of your own photos!







