What happens when you rotate an image in ImageMagick?
Rotating an image with ImageMagick’s convert command can
change the canvas size depending on the rotation angle and the specific
flags you use. By default, ImageMagick expands the canvas to fit the
rotated image’s new dimensions without losing any pixels, but you can
override this behavior to crop the image or keep the original canvas
size instead.
The Default Behavior: Automatic Canvas Expansion
When you perform a standard rotation using the -rotate
flag, ImageMagick automatically calculates the new bounding box required
to hold the rotated image. If you rotate by an angle that is not a
multiple of 90 degrees (such as 45 degrees), the corners of the image
will push outward, requiring a larger canvas.
- Canvas Dimensions: The width and height of the output image increase to accommodate the tilted corners.
- Empty Space: The newly created empty corners of the canvas are filled with a background color (white by default, though this can be customized).
For example, running the following command on a $1000 $ pixel image at a 45-degree angle will result in a larger output image of approximately $1414 $ pixels:
magick convert input.jpg -rotate 45 output.jpg
Keeping the Original Canvas Size (Cropping)
If your goal is to rotate the image within its original
dimensions without changing the canvas size, you have to explicitly tell
ImageMagick to crop the resulting image back to the original width and
height. This is achieved by using the -background and
-extent flags alongside the original dimensions, or by
using a specific offset operator.
Using the Distort Operator
A cleaner way to rotate an image while strictly preserving the
original canvas size is using the -distort operator instead
of -rotate. The ScaleRotateTranslate (SRT)
distortion method rotates the image around its center while keeping the
canvas dimensions exactly the same, naturally clipping any portions of
the image that rotate outside the original boundary.
magick convert input.jpg -distort SRT 45 output.jpg
Summary of Rotation Methods and Canvas Impacts
The following table outlines how different ImageMagick commands affect the final canvas dimensions:
| Command Approach | Impact on Canvas Size | Resulting Image Frame |
|---|---|---|
-rotate 90 / -rotate 270 |
Width and height are swapped | Full image preserved |
-rotate 45 (Standard) |
Canvas expands to fit new bounding box | Full image preserved with background filler |
-distort SRT 45 |
Canvas size remains exactly the same | Image is rotated and cropped to original dimensions |