ImageMagick Tutorial

By Xah Lee. Date: . Last updated: .

This page shows you how to do common image editing tasks using the command line software ImageMagick.

for faster alternative, see GraphicsMagick Tutorial

find version magick --version

jpg png Conversion

gif to png

magick convert p1.gif p2.png

png to jpg

magick convert -quality 90% old.png new.jpg

You can also convert to gif, tiff and other image formats.

Remember, the destination format should support all the features of the format you are converting from, otherwise you may lose info. For example, converting from png to gif may lose colors because gif only support a max of 256 colors.

Here's summary of popular formats:

Scale, Crop

scale

magick convert -scale 50% x1.jpg x2.png
magick mogrify -scale 50% x.png

autocrop

magick convert -trim x.png x.png

crop

magick convert -crop 853x368+0+56 old.png new.png

The 853 and 368 would be the new image's width and height. The 0 is the offset on x-axis, and 56 is the offset of y-axis. The x and y axes's origin starts at the upper left corner.

To crop by specifying percentage of sides to cut, use “-shave”.

Color, Brightness, Saturation

brightness

magick convert -modulate 150,100,100 old.png new.png

The above increase brightness by the multiplier 150%. To decrease, use values less than 100.

The 3 numbers means: brightness, saturation, hue. They are all interpreted as percentages.

saturation

magick convert -modulate 100,130,100 old.png new.png

The above increase color saturation by the multiplier 130%. To decrease, use values less than 100.

Transparency, Color, Bits Per Pixel

remove transparency/alpha with white

magick convert -flatten old.png new.png

to gray scale

magick convert -type Grayscale old.png new.png

Note: this does not force the png image format to use indexed color for smaller file size.

reduce bits per pixel

Use -depth. In particular, for a grayscale line art in png, you can do: magick convert -depth 8 old.png new.png. That makes it 8 bits. For clean black and white line art, you can use “-depth 2”. This is great for reducing file size. (see also “-colors”)

reduce color

magick convert -dither -colors 256 old.png new.png

(dither adds dots)

To reduce color without dithering, use +dither instead of -dither. Note that reducing colors does not necessarily reduce file size.

To reduce png file's size when reducing color, use -depth for values of {2, 3, 4, 8}.

steve jobs original steve jobs original 4bits no dither steve jobs original 4bits dither
Original; 4bits no dither; 4bits dithered

Image Filtering

sharpen

magick convert -sharpen 2 old.png new.png

blur

magick convert -blur 1 old.png new.png

Image Editing

insert copyright notice

magick convert -fill red -draw 'text 20 20 "© 2006 example.com"' old.png new.png

Use -gravity SouthEast -font helvetica to put the text in other corners, and change font.

add a border

magick convert -border 1 -bordercolor black x.png x2.png

Flip, Rotate

rotate

magick convert -rotate 90 x.png x.png

Positive degree means clockwise.

flip

# left/right flip
magick convert -flop x.png x.png

# up/down flip
magick convert -flip x.png x.png

Combine Images

combine 2 images

montage -mode concatenate -tile 1x x1.jpg x2.jpg new.jpg

or

# join images vertically
magick convert x1.png x2.png x3.png -append new.png

# join images horizontally
magick convert x1.png x2.png x3.png +append new.png

Batch Process

batch process

There are many ways to process all files in a directory in one shot. You can use the unix shell utils “find” and “xargs”.

Suppose you want to convert all files in a dir from png to jpg. Here's GNU/Linux example:.

find . -name "*png" | xargs -l -i basename "{}" ".png" | xargs -l -i  magick convert -quality 85% "{}.png" "{}.jpg"

Image Tools