Adjust the Sharpness of an Image in Python

After reading this article you will be able to adjust Sharpness of an Image in Python. For this purpose, we use the Python Imaging Library (PIL). PIL consists of various Classes. We require a few classes like Image and Image Enhance to Adjust the Sharpness of an Image in Python.

So our very first line of Python Script will be as follows

from PIL import Image,ImageEnhance

Here, Image Class is used to perform some basic operations like Opening, Cropping, Resize etc. Whereas, ImageEnhance is used to Adjust Brightness, Contrast, Sharpness, Color etc.

To know more about Image and ImageEnhance Click here -> Image and Image Enhance.

At first, we have to open an Image. So let’s have a look at following code

img=Image.open("Path_to_your_Image")

From the above, open() is a Pre-defined Method of Image Class which creates an Image Object (i.e. img) of an Existing Image at specified Path (i.e. Path_to_your_Image). To Adjust the Sharpness of an image, we need to create an Object for Sharpness which is present in ImageEnhnace Class. It can be done as follows

img_shr_obj=ImageEnhance.Sharpness(img)

Where img_shr_obj is the Object for Sharpness Class of an Image and img is the Image Object from the above code. Image Sharpness Object contains a method called enhance() which is used to enhance the Sharpness. Usually, Sharpness can be enhanced according to the Factor.

Usage of factor in Enhance Method:

  • The factor is a Floating Point Number which is used to Enhance an Image.

If Factor is “1” (i.e. factor=1) then the Sharpness remains the same. Otherwise, the Sharpness of Image will be Enhanced according to the factor as follows

  • If Factor > 1 then Sharpness Increases according to the value of Factor.
  • If Factor < 1 then Sharpness Decreases according to the value of Factor.

Let’s have a look at using enhance() method

e_i=img_shr_obj.enhance(factor)

As said, e_i is the Enhanced Image Object. It can either be Viewed or Saved. Show() method can be used to view the Enhanced Image and Save() method is used to save the Enhanced Image at Specified Path.

Syntax:

Show() Method:

>>> img.show()

Save() Method:

>>>img.save(“Destination”)

Here, img is the Image Object or Enhanced Image Object. Whereas, Destination is the Path to save the Image.

How to Adjust the Sharpness of an Image in Python

Example:

Let’s consider the following example for adjusting the Sharpness of an Image.

Input:

Our Input will be a sample Image. Let’s have the below Image as Input.

Adjust the Sharpness of an Image in Python

Let’s have a look at the following code which can View a Sharpness Enhanced Image.

from PIL import Image, ImageEnhnace
img=Image.open("Path_to_your_Image")    # Opening Image
img_shr_obj=ImageEnhance.Sharpness(img)
factor=10    # Specified Factor for Enhancing Sharpness
e_i=img_shr_obj.enhance(factor)    #Enhances Image
e_i.show()   # Shows Enhanced Image

Our Code will Enhances Sharpness by the Factor of 10 (let) and it shows the Enhanced Image. If you want to save the Enhanced Image kindly use save() method instead of show() method.

Output:

Hence, Our Output will be as follows

Adjust the Sharpness of an Image in Python

You can also read Change Brightness of an Image in Python

Leave a Reply

Your email address will not be published. Required fields are marked *