Adjust Image Brightness using Pillow - Python Examples


Python - Adjust Image Brightness using Pillow Library

You can adjust the brightness of an image using the Python Pillow library. Adjusting the brightness involves either increasing or decreasing the pixel values evenly across all channels of the entire image. Increasing the brightness makes the image appear whiter, while decreasing it makes the image darker.


Steps to Adjust Image Brightness using PIL

To adjust image brightness using Python Pillow, follow these steps:

  1. Read the image using Image.open().
  2. Create an ImageEnhance.Brightness() enhancer for the image.
  3. Enhance the image brightness using the enhance() method, by setting the required factor.

By adjusting the factor, you can brighten or darken the image. A factor of 1 retains the original brightness, while a factor greater than 1 brightens the image, and a factor less than 1 darkens the image.


Examples

1. Increase the brightness of the given image

In the following example, we will increase the brightness of the image by a factor of 1.5, resulting in a brightened image.

Python Program

from PIL import Image, ImageEnhance

# Read the image
im = Image.open("sample-image.png")

# Image brightness enhancer
enhancer = ImageEnhance.Brightness(im)

factor = 1  # Original brightness
im_output = enhancer.enhance(factor)
im_output.save('original-image.png')

factor = 1.5  # Increase brightness
im_output = enhancer.enhance(factor)
im_output.save('brightened-image.png')

Explanation:

  1. First, the image is opened using Image.open().
  2. An ImageEnhance.Brightness() enhancer is created to adjust the brightness.
  3. The brightness is first preserved with a factor of 1.
  4. Then, the brightness is increased by setting the factor to 1.5, which brightens the image.
  5. Finally, the brightened image is saved with the name brightened-image.png.

Original Image

Python Pillow - Adjust Image Brightness - Brighten the image

Brightened Image - With increased brightness

Python Pillow - Adjust Image Brightness - Brighten the image

2. Decrease the brightness of the given image

In the following example, we will decrease the brightness of the image by a factor of 0.5, resulting in a darker image.

Python Program

from PIL import Image, ImageEnhance

# Read the image
im = Image.open("sample-image.png")

# Image brightness enhancer
enhancer = ImageEnhance.Brightness(im)

factor = 1  # Original brightness
im_output = enhancer.enhance(factor)
im_output.save('original-image.png')

factor = 0.5  # Decrease brightness
im_output = enhancer.enhance(factor)
im_output.save('darkened-image.png')

Explanation:

  1. The image is opened using Image.open().
  2. An ImageEnhance.Brightness() enhancer is created to adjust the image's brightness.
  3. First, the original brightness is retained by setting the factor to 1.
  4. Then, the brightness is decreased by setting the factor to 0.5, which darkens the image.
  5. The darkened image is saved with the name darkened-image.png.

Original Image

Python Pillow - Adjust Image Brightness - Brighten the image

Darkened Image - With decreased brightness

Python Pillow - Adjust Image Brightness - Brighten the image

3. Apply a slight brightness increase

In this example, we apply a moderate brightness increase by using a factor of 1.2.

Python Program

from PIL import Image, ImageEnhance

# Read the image
im = Image.open("sample-image.png")

# Image brightness enhancer
enhancer = ImageEnhance.Brightness(im)

factor = 1  # Original brightness
im_output = enhancer.enhance(factor)
im_output.save('original-image.png')

factor = 1.2  # Slightly increase brightness
im_output = enhancer.enhance(factor)
im_output.save('slightly-brightened-image.png')

Explanation:

  1. As before, the image is opened and an ImageEnhance.Brightness() enhancer is created.
  2. We first preserve the original brightness with a factor of 1.
  3. Then, we apply a slight brightness increase with a factor of 1.2, which makes the image slightly brighter.
  4. The result is saved as slightly-brightened-image.png.

Summary

In this tutorial, we learned how to adjust the brightness of an image using the ImageEnhance.Brightness() function in the Python Pillow library. By adjusting the enhancement factor, we can brighten or darken the image based on the required outcome.


Python Libraries