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

gif to png

convert p1.gif p2.png

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

scale

convert -scale 50% old.gif new.png

autocrop

convert -trim cat.png cat.png

crop

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

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

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

convert -flatten old.png new.png

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.

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”)

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

sharpen

convert -sharpen 2 old.png new.png

blur

convert -blur 1 old.png new.png

Image Editing

insert copyright notice

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

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

Flip, Rotate

rotate

convert -rotate 90 x.png x.png

Positive degree means clockwise.

flip

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

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

Combine Images

combine 2 images

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

or

# 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

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.

Image/Video Tutorial

  1. ImageMagick
  2. Convert WEBP to JPG
  3. Convert gif to Video
  4. Convert Video mov webm mp4
  5. View meta info in Image Files
  6. What's Gamma
  7. What's HDRI Image