Free course · Lesson 2 of 5
Images Are Grids of Numbers
In this lesson you will
- Explain pixels, channels and resolution
- Reduce image size for storage and sharing
A digital image is a grid of pixels. A colour image stores three numbers per pixel, one each for red, green and blue, usually from 0 to 255. In Python, an image is a NumPy array of shape (height, width, 3).
- Coordinates start at the top-left: image[row, column], with rows going down.
- OpenCV uses BGR order: convert to RGB before displaying with other libraries, or red tomatoes look blue.
- HSV colour space separates colour (hue) from brightness, which makes selecting "yellow" or "green" more reliable when the light changes.
- Size matters: a 12-megapixel phone photo can be resized to 640 pixels wide for most learning tasks, which makes datasets small enough for a laptop and a USB stick.
Python
import cv2 img = cv2.imread("market_tomatoes.jpg") # a NumPy array in BGR orderprint(img.shape) # e.g. (3000, 4000, 3) scale = 640 / max(img.shape[:2])small = cv2.resize(img, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)print(small.shape) # (480, 640, 3) cv2.imwrite("tomatoes_small.jpg", small, [cv2.IMWRITE_JPEG_QUALITY, 85])
Practice exercise
Check the resolution and file size of three photos on your phone and estimate how much space a dataset of 1,000 such photos would need before and after resizing.
Key takeaways
- A colour image is a grid of numbers with three channels
- Resizing and compression make datasets practical on modest computers
Free self-study lesson — no certificate. For the full instructor-led program, see registration options.