Skip to main content
This guide covers essential geometric transformations including resizing, rotation, flipping, cropping, and more advanced affine and perspective transformations.

Overview

Image transformations allow you to modify the geometry of images. Common operations include:
  • Resizing images to different dimensions
  • Rotating images by any angle
  • Flipping images horizontally or vertically
  • Cropping regions of interest
  • Applying affine and perspective transformations

Resizing Images

Resize images to specific dimensions or by a scaling factor.

Interpolation Methods

MethodDescriptionUse Case
INTER_NEARESTNearest neighborFastest, lowest quality
INTER_LINEARBilinear interpolationGood balance (default)
INTER_CUBICBicubic interpolationSlower, higher quality
INTER_AREAResampling using pixel areaBest for downsampling
INTER_LANCZOS4Lanczos interpolationHighest quality, slowest

Rotating Images

Rotate images by 90-degree increments or arbitrary angles.

Simple 90-Degree Rotations

Arbitrary Angle Rotation

When rotating by arbitrary angles, parts of the image may be cropped. To preserve the entire rotated image, calculate new dimensions and adjust the rotation matrix accordingly.

Flipping Images

Flip images horizontally, vertically, or both.

Cropping Images

Crop a region of interest from an image using array slicing.

Affine Transformations

Affine transformations preserve parallel lines and include translation, rotation, scaling, and shearing.

Perspective Transformations

Perspective transformations correct for camera angle and viewpoint changes.
Perspective transformations are commonly used for document scanning, where you select the four corners of a document in a photo and transform it to a flat, rectangular view.

Key Functions

FunctionDescription
resize()Resize image to specific dimensions or scale
rotate()Rotate image by 90, 180, or 270 degrees
flip()Flip image horizontally, vertically, or both
getRotationMatrix2D()Get rotation matrix for arbitrary angles
getAffineTransform()Get affine transformation matrix from 3 point pairs
getPerspectiveTransform()Get perspective transformation matrix from 4 point pairs
warpAffine()Apply affine transformation
warpPerspective()Apply perspective transformation
When applying transformations, pixels that fall outside the destination image are cropped. Use appropriate border modes or adjust output dimensions to preserve all data.