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!
Rotating Images
Rotation is used in data augmentation (training ML models), correcting image orientation, and transforming images before analysis.
8 min•By Priygop Team•Updated 2026
Rotating with OpenCV
Rotating with OpenCV
import cv2
import numpy as np
image = cv2.imread("photo.jpg")
h, w = image.shape[:2]
# METHOD 1: Simple 90/180/270 degree rotations
# Fastest — no interpolation needed
rot_90 = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
rot_180 = cv2.rotate(image, cv2.ROTATE_180)
rot_270 = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
# METHOD 2: Arbitrary angle rotation with warpAffine
def rotate_image(image, angle_degrees):
"""Rotate image by any angle around its centre."""
h, w = image.shape[:2]
centre = (w // 2, h // 2)
# Get the rotation matrix
M = cv2.getRotationMatrix2D(centre, angle_degrees, scale=1.0)
# Apply the rotation
rotated = cv2.warpAffine(image, M, (w, h))
return rotated
rot_45 = rotate_image(image, 45)
rot_30 = rotate_image(image, 30)
rot_neg15 = rotate_image(image, -15) # Negative = counter-clockwise
cv2.imwrite("rotated_90.jpg", rot_90)
cv2.imwrite("rotated_45.jpg", rot_45)
print("Rotation complete")Diagram
Loading diagram…
Machine Learning follows a structured pipeline from data to deployment