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:
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.

