How to Resize Image in ImageMagick Ignore Aspect Ratio?
When resizing images using the ImageMagick convert
command, the software automatically preserves the original aspect ratio
by default to prevent distortion. However, you can force ImageMagick to
ignore the aspect ratio and resize an image to your exact specified
dimensions by appending an exclamation point (!) to the
geometry argument. This quick guide will show you the exact syntax
required to achieve this, how to handle the character in different
operating system terminals, and a few practical examples.
The Forced Resizing Syntax
To override the default aspect ratio preservation, you must add an exclamation point immediately after your desired width and height dimensions. Because the exclamation point is a special character in many command-line shells, the way you format the command depends on your operating system.
For Linux and macOS (Bash/Zsh)
On Unix-based systems, the exclamation point must be escaped with a
backslash (\) or enclosed in quotes so the shell does not
interpret it as a history expansion command.
- Using a backslash:
convert input.jpg -resize 300x200\! output.jpg - Using single quotes:
convert input.jpg -resize '300x200!' output.jpg
For Windows (Command Prompt)
On Windows, the exclamation point does not carry the same special meaning in the standard Command Prompt, so you can use it directly without escaping.
- Windows syntax:
convert input.jpg -resize 300x200! output.jpg
How It Works Under the Hood
When you pass a standard dimension like -resize 300x200
to ImageMagick, it treats those numbers as a maximum bounding box. The
image is scaled down until it fits entirely within a 300x200 area while
maintaining its original proportions.
By adding the !, you instruct ImageMagick to treat the
dimensions as absolute. If the original image was a square (e.g.,
500x500) and you force it into a 300x200 rectangle, the resulting image
will appear stretched or squished to match those exact pixel counts.
Summary Table of Terminal Variations
| Operating System | Terminal/Shell | Required Syntax |
|---|---|---|
| Linux / macOS | Bash, Zsh | 300x200\! or '300x200!' |
| Windows | Command Prompt (cmd) | 300x200! |
| Windows | PowerShell | '300x200!' |