Image Enhancement in PIL - GeeksforGeeks

04 May, 2022
The Python Imaging Library(PIL) adds powerful image processing capabilities. It provides immense file format support, an efficient representation, and fairly powerful image processing capabilities. The core image library is intended for fast access to data stored in very few basic pixel formats. It provides a firm foundation for a general image processing tool.
Step-1: Import the Image module from the PIL library.e
from PIL import Image
This module provides a class with an identical name which is employed to represent a PIL image. And also provides various functionalities, including functions to load images from files, and to create new images. I’m not gonna explain the whole Image module here. But, here’s how you’ll be able to open an image file.
image_variable_name = Image.open("lena.jpg")
We are going to use a PNG image here. One thing to remember- the image file you’re using here must be present within the same directory where your program is. Else use the full path of the image file within the quotation marks. 
Now you can see the image in your image viewer with a line of code.
image_variable_name.show()
Step-2: Now It is time to Import the foremost important module- ‘ImageEnhance’ module from the PIL library
from PIL import ImageEnhance
The ImageEnhance module contains various classes which will be used for image enhancement.
All enhancement classes implement a typical interface, containing one method which is named the ‘enhance(factor)’ method.
PIL.ImageEnhance.[method](image_variable_name)
The method can be brightness, color, contrast, sharpness.
Parameter: The enhance() method takes just one parameter factor, i.e. a floating-point.Return Type: This method returns an enhanced image. 
Classes are as follows:
Adjust image brightness. It’s accustomed to controlling the brightness of our resulting image. The code for brightness goes like below:
Input:
Python3
from PIL import Imagefrom PIL import ImageEnhance  # Opens the image fileimage = Image.open('gfg.png')    # shows image in image viewerimage.show()      # Enhance Brightnesscurr_bri = ImageEnhance.Brightness(image)new_bri = 2.5  # Brightness enhanced by a factor of 2.5img_brightened = curr_bri.enhance(new_bri)  # shows updated image in image viewerimg_brightened.show()  
An enhancement factor of 0.0 results in a full black image and an enhancement factor of 1.0 results the same as the original image.
Output:
Adjust image color level. It’s accustomed to controlling the color level of our resulting image. The code for coloring goes like below:
Input:
Python3
from PIL import Imagefrom PIL import ImageEnhance  # Opens the image fileimage = Image.open('gfg.png')  # shows image in image viewerimage.show()    # Enhance Color Levelcurr_col = ImageEnhance.Color(image)new_col = 2.5  # Color level enhanced by a factor of 2.5img_colored = curr_col.enhance(new_col)  # shows updated image in image viewerimg_colored.show()
An enhancement factor of 0.0 results in a full black and white image and an enhancement factor of 1.0 results the same as the original image.
Output:
Adjust image Contrast. It is accustomed to controlling the contrast of our resulting image. The code for changing contrast goes like below:
Input:
Python3
from PIL import Imagefrom PIL import ImageEnhance  # Opens the image fileimage = Image.open('gfg.png')  # shows image in image viewerimage.show()    # Enhance Contrastcurr_con = ImageEnhance.Contrast(image)new_con = 0.3  # Contrast enhanced by a factor of 0.3img_contrasted = curr_con.enhance(new_con)  # shows updated image in image viewerimg_contrasted.show()  
An enhancement factor of 0.0 results in a full grey image and an enhancement factor of 1.0 results the same as the original image.
Output:
Adjust image Sharpness. It’s accustomed to controlling the sharpness of our resulting image. The code for changing sharpness goes like below:
Input:
Python3
from PIL import Imagefrom PIL import ImageEnhance  # Opens the image fileimage = Image.open('gfg.png')  # shows image in image viewerimage.show()  # Enhance Sharpnesscurr_sharp = ImageEnhance.Sharpness(image)new_sharp = 8.3  # Sharpness enhanced by a factor of 8.3img_sharped = curr_sharp.enhance(new_sharp)  # shows updated image in image viewerimg_sharped.show()
An enhancement factor of 0.0 results in a blurred image and an enhancement factor of 1.0 results in the same as the original image and a factor > 1.0 results in a sharpened image.
Output:
These 4 classes are the most used, to enhance an image. A lot of other functionalities available there. Don’t stop your learning here, go and explore more by yourself.
Picked
Python-pil
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Python | Get unique values from a list
Create a directory in Python
Defaultdict in Python
Python | Pandas dataframe.groupby()