Skip to content

How to adjust brightness, contrast, sharpness and saturation of images in PIL (pillow) – with Python Examples

Python Tutorials

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.
from PIL import Image, ImageEnhance

file = "stairs.jpg"
img = Image.open()
img.show()
Image showing colorful stairs

.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
Here is a sample code for brightness:

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.

.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
Here is a sample code for contrast:

filter = ImageEnhance.Contrast(img)
img.filter(float)

.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
Here is a sample code for brightness:

filter = ImageEnhance.Sharpness(img)
img.filter(float)

.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
Here is a sample code for brightness:

filter = ImageEnhance.Color(img)
img.filter(float)

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 it’s brightness with a factor of 1.2 as below:


filter = ImageEnhance.Brightness(img)
new_image = img.filter(1.2)

new_image.show()

Example 2: How to add contrast to an image with PIL

In the 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()

Example 3: How to increase sharpness of an image with PIL

And here is an example that applied a sharpness of 4 to an image contained in variable img.


filter = ImageEnhance.Sharpness(img)
new_image = img.filter(4)

new_image.show()

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 method.


filter = ImageEnhance.Color(img)
new_image = img.filter(3)

new_image.show()

Resources

This PIL tutorial builds on more basic image handling concepts such as opening an image and showing an image. You can navigate to more basic and more advanced Python Lessons or our PIL Tutorials.

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 an example.

Also, we have build on the imaging basics we have learned in previous PIL (pillow) library tutorials such as open() and show() methods.