How to Resize Images to Exact Pixels with ImageMagick?
This article provides a straightforward guide on how to resize images
to specific pixel dimensions using the ImageMagick convert
command. You will learn the basic command syntax, how to force exact
dimensions while ignoring the original aspect ratio, and how to resize
images while preserving their proportions.
The Basic Resize Command (Preserving Aspect Ratio)
By default, ImageMagick tries to be helpful by preserving the original aspect ratio of your image so it doesn’t look stretched or distorted. If you specify a target width and height, it will fit the image within those bounds.
convert input.jpg -resize 800x600 output.jpgIn this example, if your original image is square, ImageMagick will resize it to $600 $ pixels instead of $800 $ to prevent distortion.
Forcing Exact Pixel Dimensions (Ignoring Aspect Ratio)
If you absolutely need the image to match your exact pixel
dimensions, regardless of distortion, you must append an exclamation
point (!) to the dimensions.
Note: Depending on your operating system terminal (like Bash on Linux/macOS), the exclamation point might need to be escaped with a backslash or enclosed in quotation marks so the terminal doesn’t misinterpret it.
Use any of the following variations to force exact dimensions:
# Using single quotes (Recommended for Linux/macOS)
convert input.jpg -resize '800x600!' output.jpg
# Escaping the exclamation point
convert input.jpg -resize 800x600\! output.jpg
# Using Windows Command Prompt (No escaping needed)
convert input.jpg -resize 800x600! output.jpgAlternative Resizing Options
If you want specific dimensions but do not want your image to stretch, you can use these alternative techniques:
- Resize by Width Only: Leave the height blank. The height will scale automatically to match.
convert input.jpg -resize 800x output.jpg- Resize by Height Only: Leave the width blank. The width will scale automatically to match.
convert input.jpg -resize x600 output.jpg- Crop to Fill Dimensions: If you want the image to
fill the exact $800 $ area without stretching, you can resize and crop
the excess edges using the
^symbol combined with-gravity centerand-extent.
convert input.jpg -resize 800x600^ -gravity center -extent 800x600 output.jpg