How to use Pillow to perform image denoising on an image?

Jul 17, 2025

Leave a message

As a Pillow supplier, I'm excited to share with you how to use Pillow, a popular Python library, to perform image denoising on an image. Image denoising is a crucial step in image processing, as it helps to improve the quality of images by removing unwanted noise. This process can enhance the visual appeal of images and make them more suitable for various applications, such as printing, web display, and analysis.

Understanding Image Noise

Before we dive into the process of image denoising, it's essential to understand what image noise is. Image noise refers to random variations in the brightness or color of an image, which can occur due to various factors, such as sensor imperfections, electrical interference, or compression artifacts. There are several types of image noise, including Gaussian noise, salt-and-pepper noise, and speckle noise. Each type of noise has its characteristics and requires different denoising techniques.

Prerequisites

To follow along with this tutorial, you'll need to have Python installed on your computer, along with the Pillow library. You can install Pillow using pip, the Python package installer, by running the following command in your terminal:

pip install pillow

Loading an Image

The first step in image denoising is to load the image you want to process. Pillow provides a simple and straightforward way to load images using the Image module. Here's an example of how to load an image:

from PIL import Image

# Open an image file
image = Image.open('path/to/your/image.jpg')

# Display the image
image.show()

In this example, we first import the Image module from the Pillow library. Then, we use the open method to open an image file located at the specified path. Finally, we use the show method to display the image.

Memory Foam PillowHousehold Pillow

Adding Noise to an Image

To demonstrate the effectiveness of image denoising, let's first add some noise to our image. We'll add Gaussian noise, which is a common type of noise that follows a Gaussian distribution. Here's an example of how to add Gaussian noise to an image:

import numpy as np
from PIL import Image

# Open an image file
image = Image.open('path/to/your/image.jpg')

# Convert the image to a NumPy array
image_array = np.array(image)

# Generate Gaussian noise
noise = np.random.normal(0, 20, image_array.shape).astype(np.uint8)

# Add the noise to the image
noisy_image_array = image_array + noise

# Clip the pixel values to the range [0, 255]
noisy_image_array = np.clip(noisy_image_array, 0, 255)

# Convert the NumPy array back to a PIL image
noisy_image = Image.fromarray(noisy_image_array)

# Display the noisy image
noisy_image.show()

In this example, we first convert the image to a NumPy array using the np.array function. Then, we generate Gaussian noise using the np.random.normal function. We add the noise to the image array and clip the pixel values to the range [0, 255] to ensure that they are valid pixel values. Finally, we convert the NumPy array back to a PIL image using the Image.fromarray method and display the noisy image.

Denoising an Image

Now that we have a noisy image, let's denoise it using Pillow. Pillow provides several methods for image denoising, including the filter method, which allows us to apply various filters to an image. One of the most commonly used filters for image denoising is the Gaussian filter, which smooths the image by reducing the high-frequency components. Here's an example of how to apply a Gaussian filter to an image:

from PIL import Image, ImageFilter

# Open the noisy image
noisy_image = Image.open('path/to/your/noisy_image.jpg')

# Apply a Gaussian filter
denoised_image = noisy_image.filter(ImageFilter.GaussianBlur(radius=2))

# Display the denoised image
denoised_image.show()

In this example, we first open the noisy image using the open method. Then, we apply a Gaussian filter to the image using the filter method and the ImageFilter.GaussianBlur filter. The radius parameter controls the strength of the filter, with a larger radius resulting in a more blurred image. Finally, we display the denoised image.

Other Denoising Techniques

In addition to the Gaussian filter, Pillow provides several other filters and techniques for image denoising. Here are some examples:

  • Median Filter: The median filter is a non-linear filter that replaces each pixel with the median value of its neighboring pixels. This filter is effective at removing salt-and-pepper noise.
from PIL import Image, ImageFilter

# Open the noisy image
noisy_image = Image.open('path/to/your/noisy_image.jpg')

# Apply a median filter
denoised_image = noisy_image.filter(ImageFilter.MedianFilter(size=3))

# Display the denoised image
denoised_image.show()
  • Bilateral Filter: The bilateral filter is a non-linear filter that smooths the image while preserving the edges. This filter is effective at removing Gaussian noise.
from PIL import Image, ImageFilter

# Open the noisy image
noisy_image = Image.open('path/to/your/noisy_image.jpg')

# Apply a bilateral filter
denoised_image = noisy_image.filter(ImageFilter.BilateralFilter(radius=5, sigma_color=150, sigma_space=150))

# Display the denoised image
denoised_image.show()

Choosing the Right Denoising Technique

The choice of denoising technique depends on the type and amount of noise in the image, as well as the specific requirements of your application. Here are some general guidelines to help you choose the right denoising technique:

  • Gaussian Noise: If your image has Gaussian noise, the Gaussian filter or the bilateral filter may be effective.
  • Salt-and-Pepper Noise: If your image has salt-and-pepper noise, the median filter is usually the best choice.
  • Speckle Noise: If your image has speckle noise, more advanced denoising techniques, such as wavelet denoising, may be required.

Conclusion

In this blog post, we've explored how to use Pillow to perform image denoising on an image. We've learned about different types of image noise, how to add noise to an image, and how to denoise an image using various filters and techniques. By applying these techniques, you can improve the quality of your images and make them more suitable for various applications.

As a Pillow supplier, we offer a wide range of Memory Foam Pillow and Household Pillow products that are designed to provide comfort and support. If you're interested in purchasing our products or have any questions, please feel free to contact us for procurement and negotiation.

References

  • Pillow documentation: https://pillow.readthedocs.io/
  • NumPy documentation: https://numpy.org/doc/
  • Image processing with Python: https://www.geeksforgeeks.org/image-processing-in-python-using-pillow-module/

Send Inquiry