Enhancing Photos With Python
Enhancing Photos With Python
If you're looking to combine your inner Ansel Adams with your inner Guido van Rossum, then read on to see how to use Python to edit images programmatically.
Join the DZone community and get the full member experience.Join For Free
Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.
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)
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)
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 anymore 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.
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!
Published at DZone with permission of Mike Driscoll , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.