Image gamma adjustment is a technique used to manipulate the brightness and contrast of an image by applying a nonlinear transformation to its pixel values. In this blog post, we will explore how to perform gamma adjustment on images using the imageio library in Python.
Installing imageio
Before we begin, let’s make sure we have the imageio library installed. You can install it using pip by running the following command:
pip install imageio
Loading and Displaying an Image
To get started, we first need to load an image using imageio. You can load an image by providing the file path to the imread
function:
import imageio
# Load image
image_path = 'path/to/image.jpg'
image = imageio.imread(image_path)
Once the image is loaded, we can display it using the imshow
function:
import matplotlib.pyplot as plt
# Display image
plt.imshow(image)
plt.axis('off')
plt.show()
Gamma Adjustment
Gamma adjustment is performed by applying a gamma correction factor to each pixel in the image. The formula for gamma adjustment is:
adjusted_pixel_value = original_pixel_value ** gamma_value
where original_pixel_value
is the original intensity value of a pixel, and gamma_value
controls the amount of adjustment applied.
To perform gamma adjustment using imageio, we can use the adjust_gamma
function:
import numpy as np
from skimage import exposure
# Define gamma value
gamma = 2.0
# Perform gamma adjustment
adjusted_image = exposure.adjust_gamma(image, gamma)
# Display the adjusted image
plt.imshow(adjusted_image)
plt.axis('off')
plt.show()
In the code above, we import exposure
module from skimage to access the adjust_gamma
function. We then pass the image
array and the desired gamma
value to perform the gamma adjustment.
Conclusion
In this blog post, we explored how to perform gamma adjustment on images using imageio in Python. Gamma adjustment is a powerful technique to enhance the brightness and contrast of an image. By adjusting the gamma value, you can achieve different effects and improve the visual quality of your images. Experiment with different gamma values to see the impact on your images!
Remember to install imageio using pip before running the examples, and make sure to have your desired image file available for loading. Happy image processing!