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!
Pixels
A pixel is the smallest unit of a digital image. Every image is made up of thousands or millions of pixels arranged in a grid.
What is a Pixel?
Pixel stands for 'picture element'. It is the smallest addressable element in a digital image.
For a grayscale image, each pixel is a single integer:
- 0 = pure black
- 255 = pure white
- Values in between = shades of grey
For a colour image, each pixel is a group of three integers:
- (0, 0, 0) = black
- (255, 255, 255) = white
- (255, 0, 0) = pure red (in RGB)
- (0, 255, 0) = pure green
- (0, 0, 255) = pure blue
- (255, 165, 0) = orange
Machine Learning follows a structured pipeline from data to deployment
Accessing and Modifying Pixels
import cv2
import numpy as np
# Create a small black image (100 x 100 pixels)
image = np.zeros((100, 100, 3), dtype=np.uint8)
# Access a single pixel at row 10, column 20
pixel = image[10, 20]
print(f"Pixel at (10,20): {pixel}") # [0 0 0] = black
# Set that pixel to red (BGR format in OpenCV)
image[10, 20] = [0, 0, 255] # Blue=0, Green=0, Red=255
print(f"After change: {image[10, 20]}") # [0 0 255]
# Set a 10x10 block of pixels to white
image[40:50, 40:50] = [255, 255, 255]
print("Set a white square at rows 40-50, columns 40-50")
# Note: OpenCV uses BGR order, not RGB
# Blue=image[r,c,0], Green=image[r,c,1], Red=image[r,c,2]