Session 5 :
Image Manipulation -
Brightness & Contrast
What is Brightness & Contrast?
● Brightness: How "light" or "dark" the whole image is.
Example: Turning up your phone’s brightness at night.
● Contrast: The difference between the darkest and brightest parts.
Example: A black cat on a white pillow (high contrast) vs. a foggy day (low
contrast).
How Computers Adjust Brightness & Contrast
1. Pixels: Images are made of tiny dots called pixels.
2. Pixel Values:
○ Grayscale: 0 (black) to 255 (white).
○ Color: Three values (Red, Green, Blue).
Formula:
New Pixel = (Old Pixel × Contrast) + Brightness
● Contrast = Scaling factor (e.g., 1.5 = increase contrast).
● Brightness = Added value (e.g., +50 = brighter).
Code for Load Image
import cv2
import numpy as np
# Load image (replace "[Link]" with your photo!)
image = [Link]("[Link]")
gray = [Link](image, cv2.COLOR_BGR2GRAY) # Convert to
grayscale
Adjust Brightness & Contrast
import cv2
import numpy as np
# Load image (replace "[Link]" with your photo!)
image = [Link]("[Link]")
gray = [Link](image, cv2.COLOR_BGR2GRAY) # Convert to grayscale
alpha = 1.5 # Contrast (1.0 = no change, >1.0 = more contrast)
beta = 30 # Brightness (0 = no change, +50 = brighter)
# Apply formula: new_pixel = alpha*old_pixel + beta
adjusted = [Link](alpha * gray + beta, 0, 255).astype(np.uint8)
Show Results
import cv2
import numpy as np
# Load image (replace "[Link]" with your photo!)
image = [Link]("[Link]" )
gray = [Link](image, cv2.COLOR_BGR2GRAY ) # Convert to grayscale
alpha = 1.5 # Contrast (1.0 = no change, >1.0 = more contrast)
beta = 30 # Brightness (0 = no change, +50 = brighter)
# Apply formula: new_pixel = alpha*old_pixel + beta
adjusted = [Link](alpha * gray + beta, 0, 255).astype(np.uint8)
[Link]("Original" , gray)
[Link]("Brighter & Sharper" , adjusted)
[Link](0) # Press any key to close
[Link] ()
Code Output :
Real-Life Examples
1. Brighten a Dark Photo: Take a dim selfie and make it sunny! ☀
2. Enhance Contrast: Turn a foggy mountain into a crisp postcard! 🏔
Try This!
1. Set alpha = 0.5 and beta = -30. What happens?
2. Use a color image (skip grayscale conversion) and see how colors change!
Why This Matters
● Social Media Filters: Apps like Instagram use similar math!
● Medical Imaging: Doctors adjust contrast to see X-rays clearly.
Your Challenge:
Take a photo of your favorite toy, adjust brightness/contrast, and share it with friends!
Write a Python Script to Modify image brightness and contrast using OpenCV
trackbars.
import cv2
# Function to apply brightness and contrast
def update(val):
brightness = [Link]('Brightness', 'Image Adjuster') - 100
contrast = [Link]('Contrast', 'Image Adjuster') - 100
adjusted = [Link](img, alpha=1 + contrast / 100.0, beta=brightness)
[Link]('Image Adjuster', adjusted)
# Load the image
img = [Link]('your_image.jpg') # Replace with your image path
# Create a window
[Link]('Image Adjuster')
# Create trackbars for brightness and contrast
[Link]('Brightness', 'Image Adjuster', 100, 200, update)
[Link]('Contrast', 'Image Adjuster', 100, 200, update)
# Initialize with original image
update(0)
# Wait until user presses a key
[Link](0)
[Link]()
Write a Python Script to Modify image brightness and contrast using OpenCV
trackbars.
How It Works:
● [Link]() reads the current position of the trackbar.
● Brightness and contrast are adjusted using [Link]() where:
○ alpha = 1 + contrast/100 controls contrast.
○ beta = brightness controls brightness.
● Trackbars are set from 0–200 with 100 as the neutral point.
Requirements:
Make sure OpenCV is installed:
pip install opencv-python