[파이썬] Pillow 히스토그램 평활화

Pillow

In image processing, histogram equalization is a technique used to enhance the contrast and improve the visibility of details in an image. By redistributing the pixel intensities to cover a wider range of values, histogram equalization can be a powerful tool for enhancing images.

Pillow, the friendly fork of the Python Imaging Library (PIL), provides a simple and easy-to-use interface for performing various image processing tasks, including histogram equalization. In this blog post, we will explore how to perform histogram equalization using Pillow in Python.

Installing Pillow

Before we proceed, we need to make sure Pillow is installed in our Python environment. To install Pillow, you can use pip, the package installer for Python, by running the following command:

pip install pillow

Loading and Displaying an Image

To start with, let’s load an image using Pillow and display it. Here’s an example:

from PIL import Image

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

# Display the original image
image.show()

Make sure to replace “path/to/image.jpg” with the actual path to your image file. Running the above code should open a window displaying the original image.

Histogram Equalization

Now, let’s move on to histogram equalization. Pillow provides a convenient method called equalize() that allows us to perform histogram equalization on an image.

To perform histogram equalization, we need to convert the image to grayscale first. Here’s an example code snippet:

from PIL import ImageOps

# Convert the image to grayscale
grayscale_image = image.convert("L")

# Perform histogram equalization
equalized_image = ImageOps.equalize(grayscale_image)

# Display the equalized image
equalized_image.show()

In the above code, we first convert the original image to grayscale using the convert() method with the “L” mode. Then, we apply histogram equalization using the equalize() method from the ImageOps module. Finally, we display the equalized image.

Conclusion

Histogram equalization is a powerful technique for enhancing image contrast and improving visibility. With the help of Pillow, performing histogram equalization in Python is straightforward.

In this blog post, we learned how to install Pillow, load and display an image using Pillow, and perform histogram equalization on an image. Feel free to experiment with different images and explore the other image processing capabilities provided by Pillow.

If you want to dive deeper into image processing using Pillow, make sure to check out the Pillow documentation and the official Pillow GitHub repository for more information and examples.

Happy coding!