how to rotate a bitmap 90 degrees
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Rotating a bitmap 90 degrees means remapping every source pixel into a new destination coordinate system. The key detail is that a 90-degree rotation swaps width and height, so the rotated image needs a different output shape from the original.
Understand the Coordinate Mapping
Assume the original bitmap has:
- width
w - height
h - coordinates
(x, y)wherexgrows to the right andygrows downward
For a clockwise rotation, a source pixel at (x, y) moves to:
- new
x = h - 1 - y - new
y = x
That means the rotated image has:
- new width
h - new height
w
The transformation matters more than the image format itself.
A Simple Python Example
Here is a pure Python version using a 2D list to show the logic clearly:
Output:
This is the same operation image libraries perform, just shown in a form that makes the coordinate mapping explicit.
Counterclockwise Rotation Uses a Different Mapping
For a 90-degree counterclockwise rotation, the mapping changes to:
- new
x = y - new
y = w - 1 - x
A Python version:
If you mix up clockwise and counterclockwise formulas, the output will still look rotated, just in the wrong direction.
Using an Image Library Is Usually Better
In real applications, you normally should not rotate raw bitmap buffers manually unless you need a custom low-level pipeline. Libraries already handle pixel format details, stride, metadata, and performance.
For example, with Pillow:
This is shorter, safer, and easier to maintain than manual buffer math.
Performance and Memory Considerations
A 90-degree rotation usually requires allocating a new image buffer because the dimensions change. In-place rotation is only straightforward for square matrices and even then is more relevant to algorithm exercises than production image code.
For large bitmaps, the main concerns are:
- memory for the destination image
- cache efficiency in pixel traversal
- preserving the correct pixel format
The algorithm itself is linear in the number of pixels, which is optimal for a full-image transform.
Bitmap Format Details Matter in Low-Level Code
When developers say "bitmap," they often mean more than a simple grid of pixels. Real bitmap formats may include:
- row padding
- channel order such as BGR versus RGB
- alpha channel data
- top-down or bottom-up row storage
If you are manipulating raw BMP bytes directly, the rotation logic must account for those details too. If you are using a decoded image object, the library usually handles them for you.
Common Use Cases
Rotating by 90 degrees appears often in:
- camera or photo orientation fixes
- UI asset transformation
- document scanning apps
- game sprites and textures
The general algorithm is the same even though the surrounding application code differs.
Common Pitfalls
- Forgetting that width and height swap after a 90-degree rotation.
- Using the clockwise formula when the intended rotation is counterclockwise.
- Trying to rotate raw bitmap bytes without accounting for row padding or channel layout.
- Reimplementing low-level pixel rotation when an image library already provides a correct method.
- Assuming in-place rotation is simple for non-square images.
Summary
- A 90-degree bitmap rotation is a coordinate remapping problem.
- Clockwise and counterclockwise rotations use different formulas.
- The output image dimensions swap width and height.
- For real applications, image libraries are usually safer than manual buffer code.
- Manual rotation is still useful for understanding the underlying pixel transformation.
Related reading
- How to rotate an image to align the text for extraction?
- How to rotate image in Swift?
- How to round an image with Glide library?
- How to run eval.py job for tensorflow object detection models
- How to run prediction using image as input for a saved model?
- How to save a BufferedImage as a File
- How to scale down a UIImage and make it crispy / sharp at the same time instead of blurry?
- How to set max_detections_per_class and max_total_detections when training for unique objects using Tensorflow Object Detection API
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.