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, though magick convert or convert (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:

Examples of Conditional Resizing

ImageMagick provides flexibility in how you define your boundaries. Here is how the > modifier behaves in different scenarios:

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"
done

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