[« Planning your acrylic case] [Main Index] [More ImageMagick: using ImageMagick from PHP scripts »]

ImageMagick: a few incantations that I find useful


A lot of people find this site looking for ImageMagick help. And there's a great manual, but it's not the most accessible piece of reading. It's definitely not aimed at a novice ImageMagick user, and provides no "how do I..." sort of assistance. So I thought I'd share some useful ImageMagick incantations.

No tutorial can really substitute for spending quality time with the manual. There's way more cool tricks than I can cover here. But here's a few that should help you get your feet wet if you're scratching your head, looking at the ImageMagick manual and wondering which end is up.

How do I resize an image with ImageMagick?

The basic operation is

mogrify -geometry '400x300' -quality 40 foo.jpg

This will resize the image to 400px wide by 300px tall, with a JPEG compression setting of 40%. OF course, you'll set the JPEG compression to something that meets your needs--try a few variations and see how it affects the image quality.

One trickier incantation I like to use is

mogrify -geometry '400x600>' -quality 40 foo.jpg

This will resize the image the biggest size that will fit inside a bounding box that's 400px wide and 600px tall. It won't alter the size of a small image, but hte quality setting will get applied. This is real useful when you have a web site with a standard like "images must be no wider than 400px, and no taller than 600px" but your source images are all different sizes.

How do I watermark an image with ImageMagick?

This one took me forever to figure out the details. But it's one of the cooler stunts, because you can composite a GIF, with transparency, onto a JPEG, and ImageMagick will "do the right thing."

composite -quality 40 -compose atop -gravity southeast logo.gif target.jpg target.jpg

This will superimpose, with transparency, logo.gif in the lower right corner of target.jpg. Once again, quality 40 sets a JPEG compression level.

These incantations work equally well with wildcards in the filenames--you can resize a dir full of JPEGs or watermark them, and this is where ImageMagick really starts to shine.

text, scripts and images copyright © 2001-2011 . All rights reserved.