Python Pillow - Enhancing Sharpness



Sharpness is a key factor in defining the clarity and detail in a photograph, and it serves as a valuable tool for emphasizing texture and visual impact.

Enhancing Sharpness is an image processing technique used to enhance an image's edges, details, and contrast, resulting in a clearer and more vivid appearance. This process can significantly enhance the quality and visibility of images that are blurry, noisy, or distorted because of factors such as camera shake, low resolution, or compression.

Enhancing Sharpness of an Image

In Python Pillow library, enhancing and adjusting the sharpness of an image can be achived by using the ImageEnhance.Sharpness() class. It allows you to adjust the sharpness of an image by applying the enhancement factor.

Below is the syntax of the ImageEnhance.Sharpness() class −

class PIL.ImageEnhance.Sharpness(image)

An enhancement factor represented as a floating-point value, is passed as an argument to the common single interface method, enhance(factor). This factor plays an important in adjusting the image's sharpness. Using a factor of 0.0 can lead to a completely blurred image, while a factor of 1.0 gives the original image. And higher values enhance the image's sharpness.

Following are the steps to achieve Sharpness Enhancement of an image −

  • Create a Sharpness enhancer object using the ImageEnhance.Sharpness() class.

  • Then apply the enhancement factor to the enhancer object using the enhance() method.

Example

Here's an example that demonstrates how to adjust the sharpness of an image by specifying a factor.

from PIL import Image, ImageEnhance

# Open an image file
image = Image.open('Images/butterfly1.jpg')

# Create a Sharpness object
Enhancer = ImageEnhance.Sharpness(image)

# Display the original image
image.show()

# Enhance the sharpness with a factor of 4.0
sharpened_image = enhancer.enhance(4.0)

# Display the sharpened image
sharpened_image.show()

Output

The Input image −

butterfly and flowers

The output sharpness enhanced image −

sharpness image

Example

In this example, you can obtain a blurred version of the input image by setting the enhancement factor to a value close to zero.

from PIL import Image, ImageEnhance

# Open an image file
image = Image.open('Images/butterfly.jpg')

# Create a Sharpness object
enhancer = ImageEnhance.Sharpness(image)

# Display the original image
image.show()

# Decrease the sharpness by using a factor of 0.05
sharpened_image = enhancer.enhance(0.05)

# Display the sharpened image
sharpened_image.show()

Output

The input image −

butterfly and flowers

The output blurred version of the input image −

blured butterfly
Advertisements