ImageEnchance: Enhance your images in Python

ImageEnhancer FeaImg

Hello, fellow coder! In this tutorial, we are going to talk about the ImageEnchance library available in Python under the Pillow library. It can be used to manipulate images in a number of ways with the help of various functions present inside the sub-library.

Also Read: Visualizing Colors In Images Using Histograms – Python OpenCV

Let’s get started!

ImageEnhance.Color() Function

This function returns an image output but with a color change. The factor value can have any value you want. The value 0 implies a black and white image and the value 1 gives back the original image.

Let’s start off by displaying the original image. I have taken a sample image of a rose, you can take any other image you want.

from PIL import ImageEnhance, Image
img = Image.open('samp.jpg')
factor = 1.0
enhancer = ImageEnhance.Color(img)
enhancer.enhance(factor).show()
ImageEnhancer Original Image
ImageEnhancer Original Image

Now, let’s try to visualize the black and white version of the same image.

from PIL import ImageEnhance, Image
img = Image.open('samp.jpg')
factor = 0.0
enhancer = ImageEnhance.Color(img)
enhancer.enhance(factor).show()
ImageEnhancer Black And White Image
ImageEnhancer Black And White Image

Were you wondering what will happen if I pass a negative value to the function? It’s quite obvious that the image will start going in a negative direction. Have a look below.

from PIL import ImageEnhance, Image
img = Image.open('samp.jpg')
factor = -1.0
enhancer = ImageEnhance.Color(img)
enhancer.enhance(factor).show()
ImageEnhancer Negative Image
ImageEnhancer Negative Image

ImageEnhance.Brightness() Function

One can also play around with the brightness of the image using the code below. All we need to do is capture the current brightness of the image using the ImageEnhance.Brightness function and then apply a new brightness factor to the image.

from PIL import ImageEnhance, Image
img = Image.open('samp.jpg')

curr_bri = ImageEnhance.Brightness(img)
new_bri = 2.0

enhancer = curr_bri.enhance(new_bri)
enhancer.show()
ImageEnhancer Changed Brightness
ImageEnhancer Changed Brightness

ImageEnhance.Contrast() Function

The factor value here when set to 0.0 will give a solid grey image and a value of 1.0 will return the original image. We will keep the value as 3 to see a high-contrast image.

from PIL import ImageEnhance, Image
img = Image.open('samp.jpg')
factor = 3.0
enhancer = ImageEnhance.Contrast(img)
enhancer.enhance(factor).show()
ImageEnhancer High Contrast Image
ImageEnhancer High Contrast Image

ImageEnhance.Sharpness() Function

You can also have fun around with the sharpness of the image with the help of this function. The factor here is set to 30 to get a much sharper image. The lower the value the blurry the image is!

from PIL import ImageEnhance, Image
img = Image.open('samp.jpg')
factor = 30.0
enhancer = ImageEnhance.Sharpness(img)
enhancer.enhance(factor).show()
ImageEnhancer Sharpen Image
ImageEnhancer Sharpen Image

Conclusion

I hope you had fun working with the ImageEnchance library in Python. Try on all the different functions with various values and be amazed at how perfect the results are!

Happy coding!

Also Read: Denoising Images in Python – A Step-By-Step Guide