Enhancing Photos with Python

Sometimes when you take a photo you will find that it isn't quite what you wanted. The picture looks great, but it's just a little too dark. Or it's a little blurry and you need to add sharpness. The sharpness issue isn't anywhere near as bad nowadays because a lot of cameras will add sharpness automatically for you after they take the photo.

Anyway, in this article, we will learn how to do the following:

  • How to adjust the brightness of a photo
  • How to change the contrast of your image
  • How to sharpen a photo

The first thing you will need is the Pillow package which you can install using pip:

pip install Pillow

Now that we have Pillow installed, we can get started!


Changing the Brightness

Personally, I think this photo looks fine, but for demonstration purposes, let's try brightening up this photo anyway. The key to enhancing photos is using Pillow's ImageEnhance module. Let's take a look:

from PIL import Image
from PIL import ImageEnhance


def adjust_brightness(input_image, output_image, factor):
    image = Image.open(input_image)
    enhancer_object = ImageEnhance.Brightness(image)
    out = enhancer_object.enhance(factor)
    out.save(output_image)

if __name__ == '__main__':
    adjust_brightness('lighthouse.jpg',
                      'output/lighthouse_darkened.jpg',
                      1.7)

Here we import the pieces we need from Pillow and then we open the input image in our function. Next we need to create an "enhancer" object. In this case we use the ImageEnhance's Brightness class and pass it our image object. Next we call the enhance() method and give it the enhancement factor. According to the Pillow documentation, you need to have a factor greater than 1.0 to add brightness to your photo. If you just give it a factor of 1.0, then it will return the original image unchanged.

If you run this code, you will get something like this:

You can also pass an enhancement factor below 1.0 down to 0.0. If you go all the way down to 0.0, you will receive a completely black image according to the documentation. Just for fun, try changing the enhancement factor in the code above to 0.7. If you do, then you will get the following result:

Now let's try adding some contrast to an image!


Adjusting the Contrast of Your Image

I have taken some really dark photos before and been able to save them by adding brightness and contrast to the photos. In this example, we will only add contrast to this lovely photo of a caterpillar. However you could easily add brightness too by combining the following code with the previous example:

from PIL import Image
from PIL import ImageEnhance

def adjust_contrast(input_image, output_image, factor):
    image = Image.open(input_image)
    enhancer_object = ImageEnhance.Contrast(image)
    out = enhancer_object.enhance(factor)
    out.save(output_image)

if __name__ == '__main__':
    adjust_contrast('caterpillar.jpg',
                    'output/caterpillar_more_contrast.jpg',
                    1.7)

This code is very similar to the brightening function. The only difference is that here we are using the Contrast class from the ImageEnhance module instead of the Brightness class. When I ran this code, I got the following enhancement:

All the classes in the ImageEnhance module behave in the same way. If you happen to try passing it an enhancement value of 1.0, you just get the original image back with no adjustments added. But if you go to a value from 0.0 - 1.0, you will reduce contrast. You can play around with the enhancement factor yourself to see what kinds of changes you can make to your own images. I tried changing it to 0.7 and ended up with this:

Now we are ready to learn about sharpening photos!


Changing the Sharpness of Photos

I rarely sharpen blurry photos any more because I rarely found it helpful. However you can sharpen a photo to make it look different in a pleasing way. Let's see how you might do that with Python and Pillow:

from PIL import Image
from PIL import ImageEnhance


def adjust_sharpness(input_image, output_image, factor):
    image = Image.open(input_image)
    enhancer_object = ImageEnhance.Sharpness(image)
    out = enhancer_object.enhance(factor)
    out.save(output_image)


if __name__ == '__main__':
    adjust_sharpness('mantis.png',
                     'output/mantis_sharpened.jpg',
                     1.7)

Once again, the only change is to use the ImageEnhance's Sharpness class instead of one of the other choices. Also as before, we need to use an enhancement factor that is greater than 1.0 to add sharpness. Here's the result:

I actually kind of like how this one turned out. You can also blur the image by using an enhancement factor that is less than 1.0, but I thought that just made for a rather dull image. Feel free to play around with the values yourself though.


Wrapping Up

The Pillow package has so many neat features for editing and enhancing your photographs using the Python programming language. It's super quick to try out too. You should give these scripts a spin and play around with the enhancement factors to see what you can do yourself. There is also a Color class that I didn't cover here that you can also use to enhance your photos. Have fun and happy coding!


Related Reading

Copyright © 2024 Mouse Vs Python | Powered by Pythonlibrary