Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Practice Project
Apply everything from this module to build a complete image processing script that loads, inspects, modifies, and saves an image.
15 min•By Priygop Team•Updated 2026
Image Processing Script
Image Processing Script
import cv2
import numpy as np
def process_image(input_path, output_path):
"""
Complete image processing pipeline using Module 2 skills.
Loads an image, inspects it, modifies it, and saves the result.
"""
# --- STEP 1: Load the image ---
image = cv2.imread(input_path)
if image is None:
print(f"Error: Could not load '{input_path}'")
return False
print("=== Image Processing Pipeline ===")
print(f"Input: {input_path}")
# --- STEP 2: Inspect dimensions ---
h, w, c = image.shape
print(f"Dimensions: {w}x{h}, {c} channels")
print(f"Memory: {image.nbytes:,} bytes")
# --- STEP 3: Analyse pixel values ---
b, g, r = cv2.split(image)
print(f"Blue mean: {b.mean():.1f}")
print(f"Green mean: {g.mean():.1f}")
print(f"Red mean: {r.mean():.1f}")
# --- STEP 4: Add a colour border ---
border_size = 20
result = image.copy()
# Top and bottom borders (green)
result[:border_size, :] = [0, 255, 0]
result[-border_size:, :] = [0, 255, 0]
# Left and right borders (green)
result[:, :border_size] = [0, 255, 0]
result[:, -border_size:] = [0, 255, 0]
# --- STEP 5: Add a watermark box ---
# Draw a semi-dark overlay in bottom-right corner
overlay = result.copy()
cv2.rectangle(overlay, (w-120, h-40), (w, h), (0, 0, 0), -1)
cv2.addWeighted(overlay, 0.5, result, 0.5, 0, result)
# --- STEP 6: Save both colour and grayscale versions ---
cv2.imwrite(output_path, result)
gray = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
gray_path = output_path.replace(".", "_gray.")
cv2.imwrite(gray_path, gray)
print(f"Saved: {output_path}")
print(f"Saved: {gray_path}")
print("=== Pipeline complete ===")
return True
# Run the pipeline
process_image("your_photo.jpg", "processed_output.jpg")Diagram
Loading diagram…
Machine Learning follows a structured pipeline from data to deployment
Key Takeaways from Module 2
- Install OpenCV with: pip install opencv-python
- Load images with cv2.imread() — always check for None return value
- Display images with cv2.imshow() in scripts, or matplotlib in Jupyter notebooks
- Save images with cv2.imwrite() — format is determined by file extension
- OpenCV uses BGR channel order, matplotlib uses RGB — always convert when displaying
- Pixel access: image[row, col] for colour, image[row, col, channel] for specific channel
- Create test images with np.zeros(), np.full(), or by manually setting pixel values
Key Takeaways
- Apply everything from this module to build a complete image processing script that loads, inspects, modifies, and saves an image.
- Install OpenCV with: pip install opencv-python
- Load images with cv2.imread() — always check for None return value
- Display images with cv2.imshow() in scripts, or matplotlib in Jupyter notebooks