[파이썬] imageio 이미지 대조비 조정

Image contrast adjustment is a commonly used technique in image processing to enhance the perception and visibility of details in an image. In this blog post, we will explore how to perform contrast adjustments using the ImageIO library in Python.

Installing ImageIO

Before we get started, we need to install the ImageIO library. Open your terminal and run the following command:

pip install imageio

Importing the Required Libraries

To begin, import the necessary libraries: imageio and matplotlib. The imageio library allows us to read and write various image formats, while matplotlib is used to display and visualize the image.

import imageio
import matplotlib.pyplot as plt

Loading the Image

To demonstrate the contrast adjustment, let’s first load an image using the imageio library.

# Load the image
image_path = "path/to/your/image.jpg"
image = imageio.imread(image_path)

# Display the original image
plt.imshow(image)
plt.axis('off')
plt.title("Original Image")
plt.show()

Adjusting Contrast

Now, let’s apply contrast adjustment to the loaded image. We can do this by manipulating the pixel values to stretch or compress the image histogram.

There are various methods for adjusting contrast, such as histogram equalization, gamma correction, and histogram stretching. In this example, we will focus on histogram stretching using the exposure.adjust_gamma() function.

from skimage import exposure

# Adjust the contrast using histogram stretching
gamma = 1.5
adjusted_image = exposure.adjust_gamma(image, gamma)

# Display the adjusted image
plt.imshow(adjusted_image)
plt.axis('off')
plt.title("Adjusted Image")
plt.show()

In the above code, we have set the value of gamma to 1.5, which controls the contrast adjustment. Higher values of gamma will increase the contrast, while lower values will decrease it. You can experiment with different gamma values to achieve the desired contrast enhancement.

Conclusion

In this blog post, we explored how to perform contrast adjustments using the ImageIO library in Python. We installed the necessary libraries, loaded an image, and applied contrast adjustment techniques using the exposure.adjust_gamma() function from the skimage module. By adjusting the contrast, we can enhance the visibility and details in images, making them more appealing and informative.

Remember to experiment with different techniques and parameters to achieve the desired contrast enhancement for your specific images. Happy coding!