image-processing
algorithms
computer-graphics
rotation
programming

Image rotation algorithm

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Image rotation is conceptually simple and implementation-heavy. You want the output image to appear rotated by some angle, but pixels live on a discrete grid, so you must decide how destination coordinates map back into the source image and how pixel values should be interpolated. The robust approach is usually inverse mapping plus interpolation.

Use Inverse Mapping, Not Forward Mapping

A common beginner mistake is to rotate each source pixel forward into a destination location. That creates holes because multiple source pixels may map near the same destination location while other destination pixels get skipped.

The safer method is inverse mapping:

  • choose a destination pixel
  • map it backward into the source image
  • sample the source at that position

This guarantees every destination pixel is assigned a value.

Rotation Around the Image Center

For a rotation by angle theta, the standard 2D rotation math is applied around the image center rather than around the origin at the top-left corner.

The practical steps are:

  • shift coordinates so the center is at zero
  • apply the inverse rotation
  • shift back into source image coordinates

Runnable Python Example

Here is a small nearest-neighbor implementation using pure Python and math:

python
1import math
2
3
4def rotate_image_nearest(image, angle_degrees):
5    height = len(image)
6    width = len(image[0])
7    angle = math.radians(angle_degrees)
8    cos_a = math.cos(-angle)
9    sin_a = math.sin(-angle)
10
11    cx = (width - 1) / 2.0
12    cy = (height - 1) / 2.0
13
14    output = [[0 for _ in range(width)] for _ in range(height)]
15
16    for y_out in range(height):
17        for x_out in range(width):
18            x = x_out - cx
19            y = y_out - cy
20
21            x_src = x * cos_a - y * sin_a + cx
22            y_src = x * sin_a + y * cos_a + cy
23
24            x_nn = round(x_src)
25            y_nn = round(y_src)
26
27            if 0 <= x_nn < width and 0 <= y_nn < height:
28                output[y_out][x_out] = image[y_nn][x_nn]
29
30    return output
31
32
33source = [
34    [0, 0, 1, 0, 0],
35    [0, 0, 1, 0, 0],
36    [1, 1, 1, 1, 1],
37    [0, 0, 1, 0, 0],
38    [0, 0, 1, 0, 0],
39]
40
41rotated = rotate_image_nearest(source, 45)
42for row in rotated:
43    print(row)

This rotates a simple binary pattern. It is easy to understand and easy to test, but nearest-neighbor sampling produces visibly jagged output.

Bilinear Interpolation Improves Quality

Nearest-neighbor is fast and simple, but it creates aliasing and blocky edges. Bilinear interpolation samples the four nearest source pixels and blends them by fractional distance.

That means the algorithm becomes:

  • compute floating-point source coordinates
  • find the surrounding source pixels
  • blend them according to fractional x and y offsets

Bilinear interpolation is usually the default compromise between quality and cost. Bicubic interpolation can look better, but it is more expensive and more complex.

Output Size Matters

Some rotations fit inside the original bounds, but many do not. If you rotate into an output image of the same size, parts of the image may be clipped. A more complete rotation algorithm computes the bounds of the rotated corners first and creates a larger destination canvas if needed.

So there are two common modes:

  • rotate in place and accept clipping
  • expand the canvas to preserve the full rotated image

The right choice depends on the application.

Precision and Background Policy

When a destination pixel maps outside the source image, you need a background rule. Common choices are:

  • black
  • white
  • transparent alpha
  • edge extension

That choice affects both appearance and later processing stages. In computer vision pipelines, the border policy can matter as much as the interpolation method.

Common Pitfalls

  • Using forward mapping and creating holes in the output.
  • Rotating around the top-left corner instead of the image center by accident.
  • Assuming nearest-neighbor quality is acceptable for all images.
  • Forgetting that large rotations may require a larger output canvas.
  • Ignoring border handling for pixels that map outside the source image.

Summary

  • A good image rotation algorithm usually uses inverse mapping.
  • Rotate around the image center unless you intentionally want another pivot.
  • Nearest-neighbor is simple, but bilinear interpolation usually looks better.
  • Decide whether the output should clip or expand to fit the rotated image.
  • Border handling and interpolation choices strongly affect final quality.

Course illustration
Course illustration

All Rights Reserved.