Images as Arrays

FoundationsBeginner1 minOct 4, 2026

What you’ll learn

  • How a grayscale or color image maps to a NumPy array
  • What shape, dtype and channel order mean in practice
  • The two pitfalls that bite everyone: BGR order and uint8 overflow

Intuition

A digital image is a grid of samples. Each sample, or pixel, stores a brightness value. A grayscale image is a 2-D array of shape (H,W)(H, W). A color image adds a third axis for channels, giving shape (H,W,3)(H, W, 3).

I:{0,…,H−1}×{0,…,W−1}→{0,…,255}I : \{0, \dots, H-1\} \times \{0, \dots, W-1\} \to \{0, \dots, 255\}

The first index is the row (yy, top to bottom) and the second is the column (xx, left to right). That is the opposite of the (x,y)(x, y) order you know from math class.

Code

import cv2
import numpy as np

img = cv2.imread("cat.jpg")          # shape (H, W, 3), dtype uint8, channels in B, G, R order
print(img.shape, img.dtype)

rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)   # convert before plotting with matplotlib
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # shape (H, W)

y, x = 120, 200
print(gray[y, x])                    # row first, then column

Exercises

  1. Load an image and print the mean value of each color channel. Which one is largest?
  2. Flip an image upside down using only array slicing.