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 or GraphicsMagick.

The following shows ImageMagick commands. If you are using GraphicsMagick, just add gm in front of the command.

To install:

sudo apt-get install imagemagick

sudo apt-get install graphicsmagick

jpg png Conversion

How to convert from gif to png?

convert p1.gif p2.png

How to convert from png to jpg?

convert -scale 50% -quality 80% 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

How to scale a image?

convert -scale 50% old.gif new.png

How to autocrop border?

convert -trim cat.png cat.png

How to crop (cutting) a image?

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…

How to increase brightness?

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.

How to increase saturation?

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

How to replace transparency/alpha with white?

convert -flatten old.png new.png

How to change color image to gray scale?

convert -type Grayscale old.png new.png

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

How to reduce bits per pixel?

Use -depth. In particular, for a grayscale line art in png, you can do: 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”)

How to reduce color?

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

How to sharpen a image?

convert -sharpen 2 old.png new.png

How to blur a image?

convert -blur 1 old.png new.png

Image Editing

How to insert copyright notice?

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

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

How to add a border?

convert -border 1 -bordercolor black cat.png cat2.png

Flip, Rotate

How to rotate a image?

convert -rotate 90 x.png x.png

Positive degree means clockwise.

How to flip a image?

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

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

Combine Images

How to combine 2 images?

montage -mode concatenate -tile 1x cat1.jpg cat2.jpg out.jpg


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

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

Batch Process

How to 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  convert -quality 85% "{}.png" "{}.jpg"

The “-l” makes it process one line at a time. The “-i” makes the “{}” to stand for file name. The “basename” strips the suffix.

View/Delete Metadata in Image Files

Linux: Command to View/Delete Metadata in Image Files

If you have a question, put $5 at patreon and message me.