- Create a new image
- Open, Show & Save images (PIL)
- Paste, Merge, Blend images (PIL)
- How to Draw shapes images (PIL)
- How to Crop an image (PIL)
- How to Add Text to images (PIL)
- How to Add Frames to images (PIL)
- How to Resize an image (PIL)
- Brightness, Contrast, Saturation
- Convert image to Grayscale | B&W
- Create Photo Collages
- Digital Image Color Modes
- Digital Image Basics
- Image Manipulation w/ PIL
- Batch Resize Multiple Images (PIL)
- Watermarking Images
- Difference Between 2 Images (PIL)
How to adjust brightness, contrast, sharpness and saturation of images in PIL (pillow) – with Python Examples
Introduction
Python PIL (pillow) library’s ImageEnhance module has lot of convenient methods that can be used to make basic image adjustments such as brightness, contrast, sharpness and saturation.
In this Python tutorial, we’re going to show you how to adjust brightness, contrast, sharpness and saturation of an image using PIL (pillow) library in Python.
- To work with images in PIL you need to first import the Image module from the PIL library in Python.
- We also need to import ImageEnhance which contains all the adjustment tools we’re going to use.
- Then let’s open an image so we can work on it.
Holy Python is reader-supported. When you buy through links on our site, we may earn an affiliate commission.
from PIL import Image, ImageEnhance
file = "stairs.jpg"
img = Image.open()
img.show()
.Brightness() method from PIL’s ImageEnhance module
To change brightness of the image we can apply ImageEnhance.Brightness()
method.
To do this we need to:
- create a brightness object first
- apply brightness to the image with a given value signifying enhancement strength.
- A value of 1.0 will preserve the original image
- Values 1.0+ will brighten the image
- A value of 0 will return a pitch-black image
filter = ImageEnhance.Brightness(img)
img.filter(float)
filter = ImageEnhance.Brightness(img)
new_image = img.filter(1.2)
new_image.show()
If you encounter an error with the path to your image because there can be operating system conflicts especially regarding directory representation.
In most cases it helps to type the file path in raw string format or use backslashes instead of regular slashes.
At this point your image will be assigned to img variable but nothing will happen until you use the .show()
method as below.
Example 1: How to add brightness to an image with PIL
After opening an image and assigning it to a variable named img, you can increase its brightness with a factor of 1.2 as below:
filter = ImageEnhance.Brightness(img)
new_image = img.filter(1.2)
new_image.show()
.Contrast() method from PIL’s ImageEnhance module
To change contrast of the image we can apply ImageEnhance.Contrast() method.
To do this we need to:
- create a contrast object first
- apply contrast to the image with a given value signifying enhancement strength.
- A value of 1.0 will preserve the original image
- Values 1.0+ will add contrast to the image
- A value of 0 will return a solid-gray image
filter = ImageEnhance.Contrast(img)
img.filter(float)
Example 2: How to add contrast to an image with PIL
In the Python example below you can see the creation of a filter object with.Contrast
method an then a new image is created with a contrast factor of 2.0.
filter = ImageEnhance.Contrast(img)
new_image = img.filter(2)
new_image.show()
.Sharpness() method from PIL’s ImageEnhance module
To change brightness of the image we can apply ImageEnhance.Brightness()
method.
To do this we need to:
- create a brightness object first
- apply brightness to the image with a given value signifying enhancement strength.
- A value of 1.0 will preserve the original image
- Values 1.0+ will brighten the image
- A value of 0 will return a pitch-black image
filter = ImageEnhance.Sharpness(img)
img.filter(float)
Example 3: How to increase sharpness of an image with PIL
And here is a Python example that applies a sharpness of 4 to an image contained in variable named img.
filter = ImageEnhance.Sharpness(img)
new_image = img.filter(4)
new_image.show()
.Color() method from PIL’s ImageEnhance module
To change brightness of the image we can apply ImageEnhance.Brightness() method.
To do this we need to:
- create a brightness object first
- apply brightness to the image with a given value signifying enhancement strength.
- A value of 1.0 will preserve the original image
- Values 1.0+ will brighten the image
- A value of 0 will return a pitch-black image
filter = ImageEnhance.Color(img)
img.filter(float)
Example 4: How to add saturation to an image with PIL
By boosting the colors of an image we can achieve a saturation effect. Here we are applying a factor of 3 with the.Color
method of ImageEnhance
module.
filter = ImageEnhance.Color(img)
new_image = img.filter(3)
new_image.show()
Resources
This PIL tutorial builds on fundamental image editing concepts such as opening an image and showing an image. You can navigate to our basic Python lessons as well as more advanced Python Lessons or our other PIL Tutorials.
Recommended Python PIL Tutorials
Basic:
- How to open, show and save images with PIL
- Digital Image Basics With PIL Library In Python
- How to Resize an Image and Get Size in Python (PIL)
- How to adjust brightness, contrast, sharpness and saturation of images (PIL)
- How to convert an image to B&W or Grayscale
- Digital Image Color Modes
You can also check out PIL (pillow) library’s official documentation.
Summary
In this Python Tutorial, we’ve seen how to adjust brightness, contrast, sharpness and color saturation of an image using ImageEnhance method.
We have also demonstrated each image processing operation with a Python example.
Also, we have build on the imaging basics we have learned in previous PIL (pillow) library tutorials such as open() and show() methods.