Wednesday, October 17, 2012

Batch operations on images: ImageMagick

Having to do the same operation on multiple images sometimes lends itself to batch processing.  There are several tools for doing this.  I'll briefly introduce you to one tool: ImageMagick.  ImageMagick is suite of command-line programs that allow you to resize, crop, convert, and do many other useful things.

To use ImageMagick, download and install the program.  (Note that during the installation you will need to add the tools to the path.)  If you want to do operations on PDF, postscript or EPS files, you will also need Ghostscript.  Open a command prompt in Windows.

I'll demonstrate how to create thumbnails from the first page of a PDF file:
convert -thumbnail x100 "File.pdf[0]" File.thumb.png
This creates a thumbnail File.thumb.png from the first page of the file File.pdf[0] instructs convert to use the first page.  If you leave off the [0] part thumbnails will be created for every page in the PDF.  The thumbnail height is 100 pixels, and the width is automatically determined.

If you want to automate this, you can use command line batch processing, MATLAB, or my favorite scripting language, Perl:
foreach my $file (glob("$ARGV[0]/*.pdf"))
{
   print "Converting $file\n";
   print `convert -thumbnail 133x100 "${file}[0]" ${file}.png`;
}

No comments:

Post a Comment