Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to change the contrast and brightness of an image using OpenCV in Python?
In OpenCV, to change the contrast and brightness of an image we use cv2.convertScaleAbs() method. This function applies linear transformation to adjust pixel values effectively.
Syntax
cv2.convertScaleAbs(image, alpha, beta)
Parameters
image ? The original input image.
alpha ? The contrast value. Use 0 < alpha < 1 for lower contrast, alpha > 1 for higher contrast.
beta ? The brightness value. Good range is [?127, 127].
Alternatively, we can use cv2.addWeighted() function for the same purpose with different parameter handling.
Input Image
We will use the following image as input for our examples:

Method 1: Using cv2.convertScaleAbs()
This method directly applies alpha (contrast) and beta (brightness) values ?
import cv2
# Read the input image
image = cv2.imread('food1.jpg')
# Define alpha and beta values
alpha = 1.5 # Contrast control
beta = 10 # Brightness control
# Apply contrast and brightness adjustment
adjusted = cv2.convertScaleAbs(image, alpha=alpha, beta=beta)
# Display the result
cv2.imshow('Original vs Adjusted', adjusted)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:

Method 2: Using cv2.addWeighted()
This method uses weighted addition to blend the image with itself ?
import cv2
# Read the input image
img = cv2.imread('food1.jpg')
# Define contrast and brightness values
contrast = 1.5 # Contrast control
brightness = 30 # Brightness control
# Apply addWeighted function
# Parameters: src1, alpha, src2, beta, gamma
adjusted = cv2.addWeighted(img, contrast, img, 0, brightness)
# Display the adjusted image
cv2.imshow('Adjusted Image', adjusted)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:

Comparison
| Method | Function | Best For | Parameter Range |
|---|---|---|---|
| Method 1 | convertScaleAbs() |
Simple linear adjustments | Alpha: 0?3, Beta: ?127 to 127 |
| Method 2 | addWeighted() |
Complex blending operations | Flexible weight parameters |
Conclusion
Use cv2.convertScaleAbs() for straightforward contrast and brightness adjustments. The cv2.addWeighted() method offers more flexibility but requires careful parameter tuning for optimal results.
