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!
Image Width and Height
Image dimensions — width and height — describe the size of an image in pixels. These are among the first things you check when working with any image.
Width, Height, and Shape
Every image has a width (horizontal number of pixels) and a height (vertical number of pixels).
In OpenCV and NumPy, image dimensions are stored as (height, width, channels) — note that height comes first, which is the opposite of what you might expect from thinking width-first.
This is because NumPy arrays use (rows, columns) ordering, and rows correspond to height (vertical) while columns correspond to width (horizontal).
Machine Learning follows a structured pipeline from data to deployment
Reading Dimensions
import cv2
image = cv2.imread("photo.jpg")
# shape returns (height, width, channels)
height, width, channels = image.shape
print(f"Height: {height} pixels")
print(f"Width: {width} pixels")
print(f"Channels: {channels}")
print(f"Aspect ratio: {width/height:.2f}")
# Total number of pixels
total_pixels = height * width
print(f"Total pixels: {total_pixels:,}")
# Important: OpenCV shape is (H, W, C) not (W, H, C)
# This trips up many beginners — remember rows before columns