ImageMagick

An ever-growing cheatsheet for ImageMagick. I add things that I think are useful but difficult to unearth or understand.

General resources that I find helpful for ImageMagick:

Contents

  1. Understanding the two main commands
  2. Understanding the wildcard selector
  3. Resize, flip, or rotate a selection of images
  4. Brighten a selection of images
  5. Make a spritesheet from a selection of images
  6. Get rid of EXIF data
  7. Change the format of a selection of images

Understanding the two main commands

ImageMagick has two main commands:

  • convert
  • mogrify

convert takes an input image and outputs an additional image based on whatever transformations you pass to it. Here’s an example that takes an input PNG image, resizes it to 50%, and then exports it as a jpg:

convert my-original-image.png -resize 50% my-new-smaller-image.jpg

mogrify takes an input command, looks for any images in the directory that match that command, and replaces any matching images. Here’s an example that looks for any PNG files starting with step- and then resizes them to 10% of their original size:

mogrify -resize 10% step-*.jpg

Understanding the wildcard selector

You might have noticed the weird * in the above command. That tells ImageMagick that any character from that point forward is a match. E.g. step-1.jpg, step-2.jpg, step-wet-paint.jpg, etc.

The * wildcard selector a common programming convention. You’ll often see it in CSS, for example.

Here’s a more basic example of the wildcard selector that accepts any JPG in the current directory:

mogrify -resize 10% *.jpg

Resize, flip, or rotate a selection of images

You don’t need ImageMagick for these transformations on macOS as there is the built-in sips command. If you still want to use ImageMagick for these transformations, here’s how:

Resize a selection of images

Use either the -resize or -r commands.

Resize by percentage:

mogrify -resize 10% step-*.jpg

Resize by pixel dimensions but keep original aspect ratio if it doesn’t match:

mogrify -resize 800x600 step-*.jpg

Resize by pixel dimensions and enforce the exact dimensions:

mogrify -resize 800x600! step-*.jpg

Resize by just the width pixel dimension, with height calculated automatically:

mogrify -resize 800 step-*.jpg

Resize by just the height pixel dimension, with width calculated automatically:

mogrify -resize x600 step-*.jpg

Brighten a selection of images

Pass in -brightness-contrast and a value between -100 and 100. Example:

mogrify -brightness-contrast 20 step-*.jpg

Make a spritesheet from a selection of images

Use -append for a vertical spritesheet and +append for a horizontal one.

convert step-*.jpg -append spritesheet.jpg

Note that ImageMagick will order images by the first digit it encounters. In other words, if I name my individual files step-1.jpg, step-2.jpg, step-3.jpg, …, step-10.jpg, step-11.jpg, step-12.jpg, ImageMagick will order them like so:

  1. step-1.jpg
  2. step-10.jpg
  3. step-11.jpg
  4. step-12.jpg
  5. step-2.jpg
  6. step-3.jpg

Prevent this by ‘padding’ each file with as many digits as you go up to. Since I’m going up to -12 in this case, which is two digits, I’d pad the names like this: step-01.jpg, step-02.jpg, and so on.

You can apparently also use the montage command (which lets you place images on a grid), although it looks problematic.

Get rid of EXIF data

EXIF data is information about an image, attached to that image. It’s how you see where in the world a photo was taken, on what camera, and so on. Use -strip to remove it. Here’s how:

convert original-image.jpg -strip private-image.jpg

-strip will also remove all these profiles, comments, and chunks: bKGD,cHRM,EXIF,gAMA,iCCP,iTXt,sRGB,tEXt,zCCP,zTXt,date.

Change the format of a selection of images

Here’s how to change all TIFF files in the current directory to JPG:

mogrify -format jpg *.tiff