[파이썬] Pillow 이미지의 노이즈 제거하기

In this blog post, we will explore how to remove noise from images using the Python library Pillow. Noise in images can often be an unwanted artifact that degrades the quality of the visual content. By implementing appropriate techniques, we can effectively reduce or eliminate noise, resulting in cleaner and more visually appealing images.

Step 1: Installing Pillow:

Before we begin, make sure you have the Pillow library installed. If not, you can install it using pip with the following command:

pip install Pillow

Step 2: Loading and Displaying the Image:

First, we need to load and display the image we want to remove noise from. We can use the Image module from Pillow to achieve this. Here is an example code snippet to load and display an image:

from PIL import Image

# Load the image
image = Image.open("path/to/image.jpg")

# Display the image
image.show()

Replace "path/to/image.jpg" with the actual path to your image file.

Step 3: Applying Noise Reduction Techniques:

Next, we will apply noise reduction techniques to the loaded image using the appropriate functions provided by Pillow. Pillow offers various filters that can be applied to the image to reduce different types of noise. Here, we will cover two commonly used filters: median filter and Gaussian filter.

Median Filter:

The median filter is useful for removing salt-and-pepper noise, which appears as randomly occurring black and white pixels. The filter replaces each pixel value with the median value of its neighboring pixels. Here is an example code snippet to apply a median filter:

from PIL import ImageFilter

# Apply median filter
filtered_image = image.filter(ImageFilter.MedianFilter(size=3))

# Display the filtered image
filtered_image.show()

In this example, we applied a median filter with a size of 3x3. You can adjust the size according to your image and noise characteristics.

Gaussian Filter:

The Gaussian filter is effective for reducing Gaussian noise, which appears as random variations in brightness. The filter applies a weighted average of neighboring pixels to smooth out the noise. Here is an example code snippet to apply a Gaussian filter:

from PIL import ImageFilter

# Apply Gaussian filter
filtered_image = image.filter(ImageFilter.GaussianBlur(radius=2))

# Display the filtered image
filtered_image.show()

In this example, we applied a Gaussian filter with a radius of 2. You can adjust the radius as per your requirements.

Conclusion:

In this blog post, we explored how to remove noise from images using the Pillow library in Python. We covered two commonly used filters: the median filter for salt-and-pepper noise and the Gaussian filter for Gaussian noise. By applying these filters, we can effectively reduce noise and improve the visual quality of our images. Pillow provides many more options and techniques for noise reduction, so feel free to explore and experiment with different filters to achieve the best results for your specific images.