I have used imagemagick as a command line tool in the past; you know,
for batch image processing and tweaking in various scenarios. It has
always come through for me and generally saved me a lot of time.
Since recently purchasing my Cannon SLR, I now have a lot of very
large (3MB jpeg) pictures to manage. Imagemagick was an easy choice
for things like shrinking to a size you can share with grandma via
email, and cropping for the perfect fit on your desktop.
I would be interested if any of you have tricks to optimize / automate
such tasks. Some of my imagemagick notes are as follows:
KNOWLEDGE IS POWER
Working with images, it is often helpful to verify what your commands
have done. Use the identify command to tell you the image format,
dimmensions, etc.: identify filename.jpg
- Very useful in making robust scripts that handle different formats or sizes.
RESIZE AN IMAGE
To resize an image you can just use a simple:
convert filename.jpg -resize 50% newfilename.jpg
- This will resize the image to 50% of the original size and maintain aspect.
To resize to a particular width and maintain aspect use the following:
convert filename.jpg -resize 800 newfilename.jpg
To resize to a particular height and maintain aspect use the following:
convert filename.jpg -resize x600 newfilename.jpg
To resize in a fit in screen type mode, specify both width and height:
convert filename.jpg -resize 800x600 newfilename.jpg
- This maintains aspect and fits the image within that square
To force the image to a certain size, or "stretch" the image, you can use:
convert filename.jpg -resize 800x600 ! newfilename.jpg
- This will result in a perfect size image, but the image may be distorted.
CROP AN IMAGE
As with resize above, you can specify width or height to crop to.
- width
- convert filename.jpg -crop 50% newfilename.jpg
- convert filename.jpg -crop 800 newfilename.jpg
- height
- convert filename.jpg -crop x50% newfilename.jpg
- convert filename.jpg -crop x600 newfilename.jpg
- both
- convert filename.jpg -crop 50%x40% newfilename.jpg
- convert filename.jpg -crop 800x600 newfilename.jpg
NOTE: the above commands will not discard anything, but rather give
multiple output files of the original in pieces no larger than what is
specified in your crop command. Think "tiles".
If you want ONLY a specific portion of the original, you can follow
the dimensions with an offset x and y (+x+y). The x is measured from
left to right by default and y is measured top to bottom. The upper
left corner is +0+0. The picture saved is then measured from the +x+y
reference point, again, the default is to the right and down.
convert filename.jpg -crop 800x600+0+0 newfilename.jpg
- This gives the specified area measured to the right and down from
the top left corner (+0+0). Everything else is discarded.
convert filename.jpg -crop 800x600+100+50 newfilename.jpg
- This moves the upper left corner of the kept picture to the right
100 and down 50. Then measures to the right 800 and down 600 for the
picture to be saved. Everything else is discarded.
COMBINE STEPS IN ORDER
You can actually combine steps in order on the commandline. Just give
the name of the output file as the last argument like so:
convert filename.jpg -resize x1024 -crop 1280x1024+0+0 filename-sm-crop.jpg
FINAL NOTE
There are a million more things that imagemagick can do... countless
formats are supported and so are conversions between formats. If you
have to process large numbers of images you should check it out.