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