Python Pillow - Reducing Noise



Reducing noise in Pillow refers to the process of applying various techniques and filters to an image to remove or decrease unwanted artifacts or irregularities that can degrade the image quality. Image noise is random variations in brightness or color in images typically caused by factors such as low light conditions, electronic interference or imperfections in the image sensor. Reducing noise is an important step in image processing to improve the overall quality of images.

Pillow (PIL) provides several built-in filters and methods for reducing noise in images. Some common techniques and filters for noise reduction in Pillow include −

  • Gaussian Blur: Applying a Gaussian blur filter to the image can help smooth out reduce noise by averaging the pixel values in the vicinity of each pixel. This creates a smoother appearance and can reduce the impact of noise. The degree of blurring can be adjusted to control the level of noise reduction.
  • Median Filter: Median filtering replaces each pixel's value with the median value of the neighboring pixels. The median filter is effective for removing salt-and-pepper noise where isolated pixels have extreme values. It replaces each pixel's value with the median value of neighboring pixels.
  • Bilateral Filter: The bilateral filter smooths the image while preserving edges. It can be effective at reducing noise while maintaining image details.
  • Denoising Algorithms: Pillow supports various denoising algorithms such as the bilateral filter, Total Variation (TV) denoising algorithm or the Non-Local Means (NLMeans) filter which can be used to reduce noise while preserving image details.
  • Thresholding: We can apply a threshold to an image to remove noise by converting pixel values below a certain threshold to black and values above the threshold to white effectively binarizing the image.

Python Pillow - Image.filter() Method

Image.filter() is a method in the Python Imaging Library (PIL) or its fork. Pillow used to apply various image filters and enhancements to an image. Filters are image processing operations that can be used to modify an image in different ways such as blurring, sharpening or enhancing certain features. This method allows us to apply various predefined filters to an image.

Pillow provides a variety of predefined filters that we can use with Image.filter(), including −

  • ImageFilter.BLUR − Applies a simple blur to the image.

  • ImageFilter.CONTOUR − Enhances the contours of objects in the image.

  • ImageFilter.DETAIL − Enhances the image's details.

  • ImageFilter.EDGE_ENHANCE − Emphasizes the edges in the image.

  • ImageFilter.EMBOSS − Adds a 3D embossing effect to the image.

  • ImageFilter.SHARPEN − Sharpens the image.

Syntax

The syntax and parameters for using Image.filter() is as follows −

output_image = input_image.filter(filter_name, filter_parameters)

Where,

  • input_image − This is the source image to which we want to apply the filter.

  • filter_name − This is a string specifying the name of the filter we want to apply. Pillow provides a variety of built-in filters and we can use one of these filter names as a string. For example "GaussianBlur," "MedianFilter," "Sharpen", etc. We can also define our custom filters by providing a kernel (a list of values) as a filter.

  • filter_parameters (optional) − Some filters may accept additional parameters that control the behavior of the filter. These parameters are specific to the particular filter being used. If the filter we are applying requires parameters we would pass them as arguments in the filter_parameters part.

Example

In this example we are trying to blur the image by passing the ImageFilter.BLUR as the input parameter to the Image.filter() method.

from PIL import Image, ImageFilter

#Open an image
input_image = Image.open("Images/flowers.jpg")

#Apply Gaussian blur to the image
output_image = input_image.filter(ImageFilter.BLUR())

#Save the resulting image
output_image.save("output Image/blur.jpg")
output_image.show()

Image to be used

flowers

Output

blur flowers

Example

Here in this example we are blurring a specified part of the image by using the ImageFilter.BoxBlur() method.

from PIL import Image, ImageFilter

#Open an image
input_image = Image.open("Images/rose.jpg")

#Apply Gaussian blur to the image
output_image = input_image.filter(ImageFilter.BoxBlur(20))

#Save the resulting image
output_image.save("output Image/blur.jpg")
output_image.show()

Image to be used

flower

Output

blur rose
Advertisements