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 Dimensions
Every Computer Vision task requires you to know the dimensions of your image. Checking dimensions is one of the most frequent operations you will perform.
6 min•By Priygop Team•Updated 2026
Working with Dimensions
Working with Dimensions
import cv2
image = cv2.imread("photo.jpg")
# image.shape returns (height, width, channels)
height, width, channels = image.shape
print(f"Height: {height}")
print(f"Width: {width}")
print(f"Channels: {channels}")
print(f"Total pixels: {height * width:,}")
print(f"Total values: {image.size:,}") # Total elements
print(f"Memory bytes: {image.nbytes:,}") # Bytes in memory
# Aspect ratio
aspect_ratio = width / height
print(f"Aspect ratio: {aspect_ratio:.2f}:1")
# Check if image is landscape or portrait
orientation = "landscape" if width > height else "portrait" if height > width else "square"
print(f"Orientation: {orientation}")
# For grayscale images (no channels dimension)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_h, gray_w = gray.shape # Only 2 values for grayscale
print(f"Grayscale dimensions: {gray_w}x{gray_h}")Diagram
Loading diagram…
Machine Learning follows a structured pipeline from data to deployment