[파이썬] imageio 이미지 샤프닝

Image sharpening is a technique used to enhance the details and edges in an image, making it appear clearer and more vibrant. In this blog post, we will explore how to use the imageio library in Python to perform image sharpening.

Installing imageio

Before we begin, let’s make sure we have imageio installed. Run the following command to install it:

pip install imageio

Loading and Displaying the Image

To get started, we first need to load an image using imageio. Here’s how you can do that:

import imageio

image_path = "path_to_your_image.jpg"
image = imageio.imread(image_path)

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

Next, let’s display the image to have a visual representation of our input:

import matplotlib.pyplot as plt

plt.imshow(image)
plt.axis('off')
plt.show()

Applying Sharpening Filter

To sharpen the image, we will use a technique called “Unsharp Masking”. It involves creating a sharpened version of the image by subtracting a blurred version of the image from the original image.

from scipy import ndimage

blurred = ndimage.gaussian_filter(image, sigma=1)
sharpened = image + (image - blurred) * 1.2

In the code above, sigma is the standard deviation of the Gaussian filter. Adjust this value based on the level of sharpening you desire.

Displaying the Sharpened Image

Finally, let’s display the sharpened image:

plt.imshow(sharpened)
plt.axis('off')
plt.show()

Putting it All Together

Here’s a complete example that combines the loading, sharpening, and displaying of an image:

import imageio
from scipy import ndimage
import matplotlib.pyplot as plt

image_path = "path_to_your_image.jpg"
image = imageio.imread(image_path)

plt.figure(figsize=(10, 5))

plt.subplot(1, 2, 1)
plt.imshow(image)
plt.axis('off')
plt.title('Original Image')

blurred = ndimage.gaussian_filter(image, sigma=1)
sharpened = image + (image - blurred) * 1.2

plt.subplot(1, 2, 2)
plt.imshow(sharpened)
plt.axis('off')
plt.title('Sharpened Image')

plt.tight_layout()
plt.show()

Make sure to replace "path_to_your_image.jpg" with the actual path to your image file.

Conclusion

By using the imageio library in Python, we have learned how to apply image sharpening techniques to enhance the details and edges in an image. Experiment with different values of sigma and weights to achieve your desired level of image sharpening.