📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials NumPy NumPy with Images

NumPy with Images

5 min read
Images are NumPy arrays of shape (H, W, 3) — flip, crop, adjust brightness, and split channels.

NumPy for Image Processing

import numpy as np
from PIL import Image

# Load image as NumPy array
img = np.array(Image.open('photo.jpg'))
print(img.shape)   # (height, width, 3) — RGB

# Grayscale
gray = np.mean(img, axis=2).astype(np.uint8)

# Flip
flipped = img[::-1]           # vertical flip
mirrored = img[:, ::-1]       # horizontal flip

# Crop
cropped = img[100:400, 50:300]

# Brightness
brighter = np.clip(img + 50, 0, 255).astype(np.uint8)

# Resize (simple downscale)
small = img[::2, ::2]         # take every other pixel

# Save
Image.fromarray(gray).save('gray.jpg')

# Channels
red   = img[:, :, 0]
green = img[:, :, 1]
blue  = img[:, :, 2]