Google
 

Sunday, November 18, 2007

Another trick using ImageMagick

Last blog was about batch resizing images using ImageMagick. But ImageMagick is more than just a resizing tool. It's kinda Adobe PhotoShop (or GIMP) for command line. You might be thinkin - what might a commandline photo editing tool be possibly use of? Well, think of resizing, watermarking and putting a nice frame to images. And to make matters worse, there are around 3000 of them and are in different formats (ie, gif, jpeg, png, bmp, etc..). Well, ImageMagick can do them all in a single command line!

ImageMagick has lots and lots of cool features. I already said, it's Adobe PhotoShop for command line and I mean it! More on ImageMagick can be found on its website www.imagemagick.org.

Okay now lets get down to the Magick.. Here's the problem:

Sometimes photos taken by digital cameras are just seem faded out.. Jus like image below.



How do we fix it? Well, the simplest answer is overlay.. This can also be done in Adobe Photoshop (or GIMP). I got this idea from here.

1. Duplicate the image layer
2. Apply layer blending mode as Overlay for the upper image.

But who does the hard work of opening Photoshop (or GIMP) and all the mess when i can do it with a single command using ImageMagick. We use composite command of ImageMagick (single line).

composite -compose overlay 
<faded_image> <faded_image> <output_bright_image>

(plz note that there <faded_image> is mentioned twice as the image is overlayed upon another instance of the same image)

eg,
composite -compose overlay colorless.jpg colorless.jpg bright.jpg

Here's before and after..

Before: Faded Image

After: ImageMagick Trick

Now who says commandline is boring?

Magic of ImageMagick

Sometime back i'd written a blog about batch image resizing in linux. The basic concept of doing that was combining djpeg/cjpeg command in linux. But, well, screw that! I've a better alternative - ImageMagick.

The main drawback of using djpeg/cjpeg command was that it didn't retain the image metadata, aka EXIF (Camera name, date, Exposure, etc). So as a better and flexible alternative, Imagemagick is our tool of the trade here..

To install Imagemagick in linux,

   apt-get install imagemagick

Imagemagick is also available for Windows, Mac, FreeBSD and any platform you can think of as it is Open Source. Go to www.imagemagick.org for more.

Now the command to resize an image is (in single line)
   convert <original_image> -resize <resize_value>
-quality <resize_value> <output_image>
where
<resize_value> can be like 50% or 800x600.. hope u got the point
<quality_value> can be like 75, 85 (ie, 0-100). This whole quality control can be taken off, in that case ImageMagick automatically sets the best value.

To resize all the images in a folder, we use the following script

#!/bin/sh

mkdir resized

for i in *.JPG
do
echo -n "Converting $i... "
convert $i -resize 50% resized/$i
echo "Done"
done

When this script is run in a folder containing images, it creates a folder named resized and puts all the resized image there, plus the EXIF data are retained too.. Cool huh?