give a python code for increasing the image brightness and contrast vc2
- User
give a python code for increasing the image brightness and contrast vc2
- Assistant
Here's an example code that can increase the image brightness and contrast using OpenCV library in Python:
import cv2 import numpy as np def increase_brightness(img, value): hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(hsv) v = cv2.add(v, value) v = np.where(v > 255, 255, v) final_hsv = cv2.merge((h, s, v)) img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR) return img def increase_contrast(img, alpha, beta): img = cv2.convertScaleAbs(img, alpha=alpha, beta=beta) return img # Read the image image = cv2.imread('path/to/your/image.jpg') # Increase brightness, value can be positive or negative image = increase_brightness(image, 50) # Increase contrast, alpha > 1 increases the contrast, 0 < beta < 100 decreases the brightness image = increase_contrast(image, 1.5, 0) # Display the modified image cv2.imshow('Modified Image', image) cv2.waitKey(0) cv2.destroyAllWindows()Make sure to replace
'path/to/your/image.jpg'with the actual path to your image file. Adjust the brightness and contrast values (50,1.5,0) inincrease_brightnessand functions respectively according to your requirements. - Assistant
Here's an alternative approach using the PIL library that can increase the image brightness and contrast:
from PIL import Image, ImageEnhance def increase_brightness(img, value): enhancer = ImageEnhance.Brightness(img) img = enhancer.enhance(value) return img def increase_contrast(img, value): enhancer = ImageEnhance.Contrast(img) img = enhancer.enhance(value) return img # Open the image image = Image.open('path/to/your/image.jpg') # Increase brightness, value can be positive or negative image = increase_brightness(image, 1.5) # Increase contrast, value > 1 increases the contrast image = increase_contrast(image, 1.5) # Display the modified image image.show()Again, make sure to replace
'path/to/your/image.jpg'with the actual path to your image file. Adjust the brightness and contrast values (1.5,1.5) inincrease_brightnessandincrease_contrastfunctions respectively according to your requirements.