ImageMagick Tutorial

Xah Lee, 2007-03.

This page shows you how to do common image editing tasks using the command line software ImageMagick. If you don't know what it is, See imagemagick↗. The official website is: http://www.imagemagick.org/

How To ...

Format Conversion

Convert from gif to png.

convert p1.gif p2.png

Convert from png to jpg. Use “-quality” for compression/quality.

convert -scale 50% -quality 80% old.png new.jpg

Scaling and Cropping

Scale a image.

convert -scale 50% old.gif new.png

Cropping (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...

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 full argument to modulate are 3 numbers in the form “x,y,z”. The x is the brightness. The y is the saturation. The z is the hue. They are all interperted as percentages.

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.

Change color image to gray scale.

convert -type Grayscale old.png new.png Note: this does not reduce the color pallete.

Reduce bits per pixel.

Use -depth. (and also -colors) In particular, for a grayscale line art in png, you can do: convert -depth 8 old.png new.png

Reduce color.

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

To reduce color without dithering, use “+dither” in place of “-dither”.

Image Filtering

Sharpen a image.

convert -sharpen 2 old.png new.png

Blur a image.

convert -blur 1 old.png new.png

Image Editing

Insert copyright notice.

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

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

Flip and Rotate

How to rotate a image?

convert -rotate 90 x.png x.png. Positive degree means counter-clockwise.

How to flip a image?

To mirror it along a vertical line, use convert -flop x.png x.png. To mirror it alone a horizontal line, use “-flip”.

Batch Processing

Batch processing.

There are many ways to process all files in a directory in one shot. You can use the unix shell utils “find” and “xargs”, or write a shell script, or use Perl, Python, scsh, emacs elisp, or use emacs to construct many convert commands with each line having one calling “convert” for a image then execute all these commands as a file. Suppose you want to convert all files in a dir from png to jpg.

gnu's unix util solution: find . -name "*png" | xargs -l -i basename -s ".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 -s” strips the suffix.

To use emacs, basically you create a temp file temp.sh, and fill out this file with command lines like “convert p1.png p1.jpg”, “convert p2.png p2.jpg”, ..., then execute this file as shell script. The emacs commands to do this are:

“Ctrl+x Ctrl+f temp.sh” to create a file, “Ctrl+u Alt+! ls *png” to insert a list of png files. Type “Ctrl+Alt+%” then “\(.+\)\.png” and “convert \1.png \1.jpg” to replace each image file to the right command string. Then “Alt+! sh temp.sh” to execute the file as shell script. Also, the commands string-rectangle (Ctrl+x r t) and kill-rectangle (Ctrl+x r k) are helpful, because they let you cut/insert a column of text. For more emacs tips, see: Emacs and Unix Tips.

For Perl and Python solutions, see Making System Calls in Perl and Python.

Misc

Other options to explore:

A complete tutorial with examples: http://www.imagemagick.org/Usage/.


Related essays:

2007-03
© 2007 by Xah Lee.