Monday, October 29, 2012

batch processing using Imagemagick

Imagemagick is a wonderful tool if you want to do batch image processing.

There are few operations which are very useful, but I tend to forget them all the time. :P

Image resizing:

$mogrify -resize HxW  *.jpg

where, HxW is image size (Height x Width). We can take any image format in place of jpg.

If we want to force the desired size,

$mogrify -resize HxW! *.jpg

I prefer using the second command to the first one.

Remember, this replaces the original image file. So you need to take a backup of the current image directory before applying this command.

Format Converstion:

For conversion of all images in current directory to another format,

$mogrify -format <newformat>  *.<currentformat>

for example,
$mogrify -format ppm *.bmp

This converts all bmp images to ppm format and saves a copy.

We can apply this command without worrying at all, since a new copy is created without losing original image format.

Autocrop Images: 

Usually when we save matlab figures, we get large undesirable white boundaries.  We may want to crop those images to fit to actual image dimensions.

$convert -trim image.jpg image.jpg

For processing images in a batch, you can use the following script:
$for i in `ls *.jpg`; do convert $i -trim `basename $i`; done

If you do not want to replace the current image, and rather make a new copy of it, do as follows:
$for i in `ls *.jpg`; do convert $i -trim cropped_`basename $i`; done

Refer this link to go through more complicated settings for the same purpose.

No comments:

Post a Comment