You can download this code by clicking the button below.
This code is now available for download.
The function is used to adjust the brightness and contrast of an image. It first opens the image file, then uses the ImageEnhance module's Brightness and Contrast enhancers to adjust the brightness and contrast of the image, and finally displays the adjusted image and returns it.
Technology Stack : Pillow library, Image module, ImageEnhance module
Code Type : Image processing
Code Difficulty : Intermediate
from PIL import Image, ImageEnhance
def adjust_brightness_and_contrast(image_path, brightness_factor, contrast_factor):
with Image.open(image_path) as img:
enhancer = ImageEnhance.Brightness(img)
brightened_img = enhancer.enhance(brightness_factor)
enhancer = ImageEnhance.Contrast(brightened_img)
contrast_enhanced_img = enhancer.enhance(contrast_factor)
contrast_enhanced_img.show()
return contrast_enhanced_img