5 min read

Adjusting brightness and contrast

One often needs to tweak the brightness and contrast level of an image. For example, you may have a photograph that was taken with a basic camera, when there was insufficient light. How would you correct that digitally? The brightness adjustment helps make the image brighter or darker whereas the contrast adjustments emphasize differences between the color and brightness level within the image data. The image can be made lighter or darker using the ImageEnhance module in PIL. The same module provides a class that can auto-contrast an image.

Time for action – adjusting brightness and contrast

Let’s learn how to modify the image brightness and contrast. First, we will write code to adjust brightness. The ImageEnhance module makes our job easier by providing Brightness class.

  1. Download image 0165_3_12_Before_BRIGHTENING.png and rename it to Before_BRIGHTENING.png.
  2. Use the following code:
    1 import Image
    2 import ImageEnhance
    3
    4 brightness = 3.0
    5 peak = Image.open( "C:imagesBefore_BRIGHTENING.png ")
    6 enhancer = ImageEnhance.Brightness(peak)
    7 bright = enhancer.enhance(brightness)
    8 bright.save( "C:imagesBRIGHTENED.png ")
    9 bright.show()
  3. On line 6 in the code snippet, we created an instance of the class Brightness. It takes Image instance as an argument.
  4. Line 7 creates a new image bright by using the specified brightness value. A value between 0.0 and less than 1.0 gives a darker image, whereas a value greater than 1.0 makes it brighter. A value of 1.0 keeps the brightness of the image unchanged.
  5. The original and resultant image are shown in the next illustration.
    Comparison of images before and after brightening.

Python Multimedia

  • Let’s move on and adjust the contrast of the brightened image. We will append the following lines of code to the code snippet that brightened the image.
    10 contrast = 1.3
    11 enhancer = ImageEnhance.Contrast(bright)
    12 con = enhancer.enhance(contrast)
    13 con.save( "C:imagesCONTRAST.png ")
    14 con.show()
  • Thus, similar to what we did to brighten the image, the image contrast was tweaked by using the ImageEnhance.Contrast class. A contrast value of 0.0 creates a black image. A value of 1.0 keeps the current contrast.
  • The resultant image is compared with the original in the following illustration.
    The original image with the image displaying the increasing contrast.

    Python Multimedia

  • In the preceding code snippet, we were required to specify a contrast value. If you prefer PIL for deciding an appropriate contrast level, there is a way to do this. The ImageOps.autocontrast functionality sets an appropriate contrast level. This function normalizes the image contrast. Let’s use this functionality now.
  • Use the following code:
    import ImageOps
    bright = Image.open( "C:imagesBRIGHTENED.png ")
    con = ImageOps.autocontrast(bright, cutoff = 0)
    con.show()
  • The highlighted line in the code is where contrast is automatically set. The autocontrast function computes histogram of the input image. The cutoff argument represents the percentage of lightest and darkest pixels to be trimmed from this histogram. The image is then remapped.

What just happened?

Using the classes and functionality in ImageEnhance module, we learned how to increase or decrease the brightness and the contrast of the image. We also wrote code to auto-contrast an image using functionality provided in the ImageOps module.

Tweaking colors

Another useful operation performed on the image is adjusting the colors within an image. The image may contain one or more bands, containing image data. The image mode contains information about the depth and type of the image pixel data. The most common modes we will use are RGB (true color, 3×8 bit pixel data), RGBA (true color with transparency mask, 4×8 bit) and L (black and white, 8 bit).

In PIL, you can easily get the information about the bands data within an image. To get the name and number of bands, the getbands() method of the class Image can be used. Here, img is an instance of class Image.

>>> img.getbands()
('R', 'G', 'B', 'A')

Time for action – swap colors within an image!

To understand some basic concepts, let’s write code that just swaps the image band data.

  1. Download the image 0165_3_15_COLOR_TWEAK.png and rename it as COLOR_TWEAK.png.
  2. Type the following code:
    1 import Image
    2
    3 img = Image.open( "C:imagesCOLOR_TWEAK.png ")
    4 img = img.convert('RGBA')
    5 r, g, b, alpha = img.split()
    6 img = Image.merge( "RGBA ", (g, r, b, alpha))
    7 img.show()
  3. Let’s analyze this code now. On line 2, the Image instance is created as usual. Then, we change the mode of the image to RGBA.

    Here we should check if the image already has that mode or if this conversion is possible. You can add that check as an exercise!

  4. Next, the call to Image.split() creates separate instances of Image class, each containing a single band data. Thus, we have four Image instances—r, g, b, and alpha corresponding to red, green, and blue bands, and the alpha channel respectively.
  5. The code in line 6 does the main image processing. The first argument that Image.merge takes mode as the first argument whereas the second argument is a tuple of image instances containing band information. It is required to have same size for all the bands. As you can notice, we have swapped the order of band data in Image instances r and g while specifying the second argument.
  6. The original and resultant image thus obtained are compared in the next illustration. The color of the flower now has a shade of green and the grass behind the flower is rendered with a shade of red.

    Please download and refer to the supplementary PDF file Chapter 3 Supplementary Material.pdf. Here, the color images are provided that will help you see the difference.

    Original (left) and the color swapped image (right).

    Python Multimedia

What just happened?

We accomplished creating an image with its band data swapped. We learned how to use PIL’s Image.split() and Image.merge() to achieve this. However, this operation was performed on the whole image. In the next section, we will learn how to apply color changes to a specific color region.

LEAVE A REPLY

Please enter your comment!
Please enter your name here