How to Shrink Image Only If Larger in ImageMagick?
When optimizing images for the web or saving storage space, you often need to resize large files without accidentally stretching smaller ones. ImageMagick, a powerful command-line tool, offers a simple syntax to handle this conditionally. By appending a special execution character to your geometry arguments, you can instruct ImageMagick to only downscale images that exceed your target dimensions while leaving smaller images completely untouched.
The Magic Character:
The Greater-Than Sign (>)
The secret to conditional resizing in ImageMagick is the
> symbol. When added to the end of your target
dimensions, it acts as a conditional modifier meaning “resize only if
the original image dimensions are larger than the specified
geometry.”
Basic Command Syntax
To resize an image only if it exceeds 1920x1080 pixels, use the following syntax:
magick convert input.jpg -resize "1920x1080>" output.jpg
Note for Modern ImageMagick: In ImageMagick v7 and later, the recommended command is simply
magick, thoughmagick convertorconvert(v6) will still work depending on your installation.
Why Quotation Marks Matter
In almost all command-line environments (like Linux Bash, macOS
Terminal, or Windows PowerShell), the > symbol is a
reserved character used for output redirection. If you omit the
quotation marks, the terminal will mistake your command as an
instruction to write data into a file named 1080>.
Always enclose the dimensions and the modifier in quotes:
- Linux/macOS: Use single or double quotes:
"-resize 1920x1080>"or-resize "1920x1080>" - Windows Command Prompt: Escape the symbol using the
caret character or use quotes:
-resize "1920x1080>"or-resize 1920x1080^>
Examples of Conditional Resizing
ImageMagick provides flexibility in how you define your boundaries.
Here is how the > modifier behaves in different
scenarios:
- Proportional Scaling (Bounding Box):
magick input.jpg -resize "800x600>" output.jpgThis fits the image within an 800x600 box only if it is larger than 800px wide or 600px high. The original aspect ratio is strictly maintained. - Width-Only Restriction:
magick input.jpg -resize "1200x>"output.jpg` This shrinks the image to a width of 1200 pixels only if the original width is greater than 1200 pixels. The height adjusts proportionally. - Height-Only Restriction:
magick input.jpg -resize "x768>"output.jpg` This shrinks the image to a height of 768 pixels only if the original height is greater than 768 pixels. The width adjusts proportionally.
Batch Processing Entire Directories
If you have a folder full of mixed-size images and want to safely downscale only the oversized ones, you can combine the conditional modifier with a batch command.
On Linux / macOS (Bash)
for img in *.jpg; do
magick "$img" -resize "2000x2000>" "optimized_$img"
doneOn Windows (PowerShell)
Get-ChildItem *.jpg | ForEach-Object {
magick $_.Name -resize "2000x2000>" "optimized_$($_.Name)"
}Using this method ensures that your high-resolution photos are compressed to manageable sizes, while your small icons, thumbnails, and badges remain perfectly crisp and unattered.