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!
Resizing Images
Resizing is one of the most common image processing operations. Neural networks require fixed-size inputs, and resizing is how you standardise images before feeding them into models.
8 min•By Priygop Team•Updated 2026
Resizing with cv2.resize()
Resizing with cv2.resize()
import cv2
image = cv2.imread("photo.jpg")
h, w = image.shape[:2]
print(f"Original: {w}x{h}")
# Resize to exact dimensions
resized_exact = cv2.resize(image, (640, 480))
# Resize to 50% of original
half = cv2.resize(image, (w//2, h//2))
# Resize using scale factors (fx, fy)
scaled = cv2.resize(image, None, fx=0.5, fy=0.5)
# Resize to fixed width, preserve aspect ratio
target_width = 400
ratio = target_width / w
target_height = int(h * ratio)
aspect_preserved = cv2.resize(image, (target_width, target_height))
# Common sizes for ML models
model_input = cv2.resize(image, (224, 224)) # ResNet, MobileNet
yolo_input = cv2.resize(image, (416, 416)) # YOLO
# INTERPOLATION METHODS:
# cv2.INTER_LINEAR — bilinear, good for enlarging (default)
# cv2.INTER_AREA — area-based, best for shrinking
# cv2.INTER_CUBIC — bicubic, high quality but slower
shrunk = cv2.resize(image, (w//4, h//4), interpolation=cv2.INTER_AREA)
print("All resize operations complete")Diagram
Loading diagram…
Machine Learning follows a structured pipeline from data to deployment