Smoothing Effect on Image with Pillow in Python

Pillow – Smoothing effect on image

We can apply smoothing effect on an image with Pillow in Python, by calling PIL.Image.filter() function on the given image and passing the predefined filter PIL.ImageFilter.SMOOTH, PIL.ImageFilter.SMOOTH_MORE, or a custom kernel filter that smoothens the image.

Steps to smoothen an image

The steps to smoothen a given image using predefined smoothening filter are

  1. Import Image, and ImageFilter modules from Pillow library.
  2. Read input image using Image.open() function.
  3. Call filter() function on the read Image object and pass ImageFilter.SMOOTH as argument to the function, or if you want more of this effect, then you may pass ImageFilter.SMOOTH_MORE. The function returns smoothened image as PIL.Image.Image object.
  4. Save the resulting image to required location, or you may process the image further.

Steps to smoothen a given image using custom filter.

  1. Import Image, and ImageFilter modules from Pillow library.
  2. Read input image using Image.open() function.
  3. Create a smoothening kernel filter using ImageFilter.Kernel.
  4. Call filter() function on the read Image object and pass ImageFilter.Kernel object as argument to the function. The function returns smoothened image as PIL.Image.Image object.
  5. Save the smoothened image to required location using Image.save() function.

Examples

1. Smoothen an image using ImageFilter.SMOOTH

In the following example, we read an image test_image.jpg into an Image object. Then, we smoothen this image using Image.filter() function and ImageFilter.SMOOTH filter.

We save the resulting image to a file smoothened_image.jpg.

Python Program

from PIL import Image, ImageFilter

# Open the image
image = Image.open("test_image.jpg")

# Apply smoothening filter on the image
smoothened_image = image.filter(ImageFilter.SMOOTH)

# Save the smoothened image
smoothened_image.save("smoothened_image.jpg")
Copy

Original Image [test_image_house.jpg]

Smoothing Effect on Image with Pillow in Python

Output Image [smoothened_image.jpg]

Smoothen an image using ImageFilter.SMOOTH in Pillow Python

2. Smoothen image using ImageFilter.SMOOTH_MORE

In the following example, we read an image test_image_house.jpg into an Image object. Then, we smoothen this image using Image.filter() function and ImageFilter.SMOOTH_MORE filter.

We save the resulting image to a file smoothened_image.jpg.

Python Program

from PIL import Image, ImageFilter

# Open the image
image = Image.open("test_image_house.jpg")

# Apply smoothening filter on the image
smoothened_image = image.filter(ImageFilter.SMOOTH_MORE)

# Save the smoothened image
smoothened_image.save("smoothened_image.jpg")
Copy

Original Image [test_image.jpg]

Smoothing Effect on Image with Pillow in Python

Output Image [smoothened_image.jpg]

Smoothen image using ImageFilter.SMOOTH_MORE in Pillow Python

3. Smoothen image using custom kernel filter

Custom Kernel to smoothen an image.

kernel = [2, 4, 2,
          4, 8, 4,
          2, 4, 2]
Copy

The weights are given to pixels such that the weight decreases when you move away from the center pixel. You may change the weights in the above kernel matrix, as per your requirement.

Also, you have specify the scale parameter in filter() function with the sum of values in the kernel matrix.

Please note that the kernel is a one dimensional array. If you are using a two dimensional array or a 2D numpy array, you may have to flatten the kernel.

In the following example, we pass a custom kernel filter to Image.filter() function, and smoothen the image.

Python Program

from PIL import Image, ImageFilter

# Open the image
image = Image.open("test_image_house.jpg")

# Smoothening kernel
kernel = [2, 4, 2,
          4, 8, 4,
          2, 4, 2]

kernel_filter = ImageFilter.Kernel((3, 3), kernel, scale=32, offset=0)

# Apply smoothening filter on the image
smoothened_image = image.filter(kernel_filter)

# Save the smoothened image
smoothened_image.save("smoothened_image.jpg")
Copy

Smoothened Image [smoothened_image.jpg]

Smoothen image using custom kernel filter in Pillow Python

Summary

In this Python Pillow Tutorial, we learned how to smoothen given image using PIL.Image.filter() function, with the help of examples.

Related Tutorials

Code copied to clipboard successfully 👍