Contents
Python Pillow – Sharpen Image
You can change the sharpness of the image using ImageEnhance class of PIL library.
In this tutorial, we shall learn how to sharpen an image, or blur an image using ImageEnhance class of Python Pillow(PIL) library, with the help of some well illustrated examples.
Steps to Sharpen Image using PIL
To adjust image sharpness using Python Pillow,
- Read the image using Image.open().
- Create ImageEnhance.Sharpness() enhancer for the image.
- Enhance the image sharpness using enhance() method, by the required factor.
By adjusting the factor you can sharpen or blur the image.
While a factor of 1 gives original image. factor>1 sharpens the image while factor<1 blurs the image.
Examples
1. Increase sharpness of given image
In the following example, we will increase the sharpness of given by a factor of 2, which gives a sharpened image.
Python Program
from PIL import Image, ImageEnhance
im = Image.open("original-image.png")
enhancer = ImageEnhance.Sharpness(im)
factor = 1
im_s_1 = enhancer.enhance(factor)
im_s_1.save('original-image-1.png');
factor = 2
im_s_1 = enhancer.enhance(factor)
im_s_1.save('sharpened-image.png');
CopyOriginal Image – original-image.png
Sharpened Image – sharpened-image.png
2. Decrease sharpness of given image
In the following example, we will decrease the sharpness of given by a factor of 0.05, which gives a blurred image.
Python Program
from PIL import Image, ImageEnhance
im = Image.open("original-image.png")
enhancer = ImageEnhance.Sharpness(im)
factor = 1
im_s_1 = enhancer.enhance(factor)
im_s_1.save('original-image-1.png');
factor = 0.05
im_s_1 = enhancer.enhance(factor)
im_s_1.save('blurred-image.png');
CopyOriginal Image – original-image.png
Blurred Image – blurred-image.png
Summary
In this tutorial of Python Examples, we learned how to adjust the sharpness of the image using ImageEnhance.Sharpness() function.