keepcalculator

06 Numbers and Powers

ImageMagick: Resize, Rotate & Flip Images in Terminal

Card No. 06·67162 · Updated · Standard formula, shown below

Yes, you can immediately resize, rotate, and flip images directly from the command line using ImageMagick’s convert or magick commands. You enter the input file, apply one or more operations, and specify an output file; the command runs instantly and returns the modified image. This article explains the exact syntax, common options, and practical examples for batch processing.

What Is ImageMagick?

ImageMagick is a free, open-source software suite for displaying, converting, and editing raster image files. It works on Linux, macOS, and Windows, and it provides a rich set of command-line tools. The core command for image manipulation is convert (or magick in newer versions), which reads an input image, applies a series of operations, and writes the result to an output file. Because it is scriptable, it is ideal for automating repetitive tasks like resizing hundreds of photos or rotating a batch of images from a camera.

Basic Syntax and Common Operations

The general form of the command is:

convert input.jpg [options] output.jpg

For resizing, the most common option is -resize, which takes a geometry string like 800x600 (width x height). To rotate, use -rotate followed by a degree value (positive for clockwise). To flip horizontally or vertically, use -flop (mirror left-right) or -flip (mirror top-bottom). You can combine multiple operations in one command; they are applied in the order they appear.

  • convert photo.jpg -resize 800x600 resized.jpg – scales to fit within 800×600, preserving aspect ratio.
  • convert photo.jpg -rotate 90 rotated.jpg – rotates 90 degrees clockwise.
  • convert photo.jpg -flop mirrored.jpg – flips horizontally.
  • convert photo.jpg -flip flipped.jpg – flips vertically.

How to Choose the Right Geometry

The -resize geometry can be specified in several ways. The default behavior is to resize so that the image fits within the given rectangle while maintaining the aspect ratio. This means the image will be scaled down (or up) until one dimension matches the specified value, and the other dimension will be smaller. If you want to force an exact size (which may distort the image), append ! to the geometry, like 800x600!. To only shrink or only enlarge, add > or < respectively. For example, -resize 800x600> will only shrink images larger than that size, leaving smaller ones unchanged.

Batch Processing Multiple Files

One of the biggest advantages of the command line is the ability to process many files at once. Using a loop in your shell, you can apply the same operation to every image in a folder. For instance, to resize all JPEG files in the current directory:

for file in *.jpg; do convert "$file" -resize 800x600 "resized-$file"; done

This creates new files prefixed with resized-, leaving the originals untouched. You can also use ImageMagick’s mogrify command to modify files in place, but be careful—that overwrites the originals. Always test on a copy first.

Common Problems and Solutions

One frequent issue is that the output file format is not what you expect. ImageMagick determines the format from the file extension, so if you write output.png, it will produce a PNG. If you want to force a format, use -format or specify the extension explicitly. Another common mistake is forgetting that -rotate can change the dimensions; rotating a 800×600 image by 90 degrees will produce a 600×800 image, which is correct. If you want to rotate without changing the canvas size, use -rotate with < or > to only rotate if needed, but that is rarely necessary.

If you encounter an error like unable to open image, double-check the file path and that the file exists. For large images, you might run out of memory; ImageMagick has a -limit option to control memory usage, but the default is usually sufficient for typical photos.

Comparing ImageMagick with Other Tools

There are other command-line tools for image manipulation, such as ffmpeg (primarily for video but can handle images), GraphicsMagick (a fork of ImageMagick with a similar interface), and exiftool (for metadata, not pixel editing). ImageMagick is the most feature-rich and widely used for scripting. It supports over 100 image formats, including TIFF, WebP, and SVG. For simple tasks, you might also use built-in OS tools, but they are not scriptable or as flexible.

Safety and Best Practices

Always keep a backup of your original images. When using mogrify, the changes are applied directly to the files, and there is no undo. For convert, you are creating a new file, so the original remains safe. However, if you use the same output filename as the input, you will overwrite the original—avoid that unless you are certain. Also, be aware that resizing can reduce image quality if you upscale; it is better to keep the original resolution and only downscale. For lossy formats like JPEG, re-encoding introduces generation loss, so save in a lossless format like PNG if you plan to edit repeatedly.

Frequently Asked Questions

How do I resize an image to a specific width while keeping the aspect ratio?

Use -resize 800 (just the width) or -resize 800x. ImageMagick will scale the height proportionally. For example, convert photo.jpg -resize 800x output.jpg makes the width 800 pixels and adjusts the height automatically.

Can I rotate an image by 180 degrees?

Yes, use -rotate 180. This flips the image upside down and left-right. It is equivalent to applying both -flip and -flop.

How do I flip an image horizontally without changing the file size?

Use -flop (for horizontal) or -flip (for vertical). These operations do not change the dimensions or file size, only the orientation of the pixels.

Is there a way to process images in subfolders?

Yes, you can use find to locate files and then loop over them. For example: find . -name '*.jpg' -exec convert {} -resize 800x600 resized-{} \;. This processes all JPEGs in the current directory and its subdirectories, creating new files with the prefix resized-.

Does ImageMagick work with HEIC files?

ImageMagick can read HEIC if it was compiled with libheif support. Many distributions include this by default, but if you get an error, you may need to install the appropriate package (e.g., libheif1 on Debian/Ubuntu). Alternatively, convert HEIC to JPEG first using a dedicated tool.

What is the difference between convert and magick?

In ImageMagick 7, magick is the unified command that replaces convert, identify, and others. If you have version 6, use convert. The syntax is largely the same, but magick is the recommended command for new scripts.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →