Saturday, April 17, 2010

ImageMagick Tutorial

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

Autocrop border

convert -trim cat.png cat.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 interpreted 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.
Color, bits per pixel, file size.

Change color image to gray scale.

convert -type Grayscale old.png new.png

Note: this does not necessarily force image format use indexed color to reduce 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.

To reduce color without dithering, use “+dither” in place of “-dither”. Note that reducing colors does not necessarily reduce file size.
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 bash shell script, or use Perl, Python, emacs.

Suppose you want to convert all files in a dir from png to jpg.

unix shell solution (programs assume GNU version):

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:

* -annotate
* -comment

* -contrast-stretch
* -black-threshold
* -white-threshold
* -level
* -modulate
* -monochrome
* -normalize
* -quantize

* -blur
* -radial-blur
* -adaptive-blur
* -unsharp
* -adaptive-resize
* -adaptive-sharpen
* -despeckle
* -dither
* -enhance
* -equalize
* -gaussian
* -antialias

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

No comments:

Post a Comment