[파이썬] imageio 이미지 밝기 조절

imageio

Sometimes, when working with images, you may come across the need to adjust their brightness. In this blog post, we will explore how to use the imageio library in Python to adjust the brightness of an image.

Installing imageio

To get started, we need to install the imageio library. You can install it using pip by running the following command:

pip install imageio

Loading an Image

First, we need to load an image using imageio. The library supports various formats including JPEG, PNG, TIFF, and more.

import imageio

# Load the image
image = imageio.imread('path_to_image.jpg')

Replace 'path_to_image.jpg' with the actual path to your image.

Adjusting Brightness

To adjust the brightness of the image, we can simply multiply each pixel value by a brightness factor. A brightness factor greater than 1 will increase the brightness, while a factor less than 1 will decrease it.

import numpy as np

# Adjust brightness
brightness_factor = 1.5  # Increase brightness by 50%
brightened_image = np.clip(image.astype(int) * brightness_factor, 0, 255).astype(np.uint8)

In the code above, we’re using the numpy library to perform the multiplication. The clip function is used to ensure that the pixel values stay within the valid range of 0 to 255.

Saving the Adjusted Image

Finally, we can save the adjusted image using imageio.

# Save the adjusted image
imageio.imsave('path_to_output_image.jpg', brightened_image)

Replace 'path_to_output_image.jpg' with the path and desired file name for the adjusted image.

Conclusion

Using the imageio library in Python, we can easily adjust the brightness of an image. By multiplying each pixel value by a factor, we can control the brightness level. This can be useful in various image processing tasks and can enhance the visual appeal of your images.

Give it a try and see how you can play around with the brightness of your images! Happy coding!

Note: Don’t forget to import the necessary libraries and replace the file paths in the code examples with the actual paths relevant to your situation.