Welcome GuestLogin

A Drunken Wiki

RSS RSS

A Drunken Wiki is the companion wiki to the blog "A Drunken Madman", written by Jason Brown.

Navigation




Search the wiki
»

Vaccination Saves Lives: Stop The Australian Vaccination Network


Find me on Twitter, Facebook, Google+, Pinterest and LinkedIn





Pin It






Alpha Archive Atheism Blue Mountains Climbing Cycling Development DotNet GPS Guitar HMHB JavaScript JQuery LINQ Meta MTB Music Perl Powershell RNP SharePoint Skepticism Social Media Training Ukulele Vaccines Visual Studio WebParts Woodford Festival 2011 XML

PoweredBy

Bulk resizing of images with Image::Magick

RSS
Modified on 2012/01/04 07:20 by Administrator Categorized as Archive, Development, Perl
This is an archive article from InfiniteMonkeys Web Programming Resource

Bulk resizing of images with Image::Magick
by : Atrax

The bit of being a web developer is dislike most is the constant image work that crops up. Working in a design studio often means you can palm it off to a designer or artworker, but it still means someone gets to be bored witless reworking a few hundred images. Why not automate it? Here's a little script I whipped up to do exactly that.

I'm currently sitting at home, working on a site which we rebranded, and handed off to a different development company to build. Why am I working on it? Well, turns out the other developers weren't quite up to the re-work. It's proving to be ripe material for articles for this site, some of which are on what NOT to do ;-). This one comes from the fact that the old site contains several hundred images which are, guess what? The wrong size. What I need to do is take 187 images and resize them from 80px X 80px to 40px X 40px. Now I could do this a few ways.

I could hand them off to a designer or artworker
I could slap them into fireworks and use the 'batch' feature
I could spend a few hours in photoshop pushing my mouse around getting bored

Well, I'm at home, not at the office, so the designer/artworker option is a bit tricky. Fireworks' batch mode only works on PNGs, which is a pain. The third option isn't one I relish. Of course, being a Perl Scripter, I have a fourth option.

First, if you haven't already got it, you'll need to install ImageMagick, with PerlMagick support as an option. I already have it installed, so I'll skip this step. If you can't figure out how to run a Win32 installer there's something up with you!

Then, I just grab the images I want to resize, paste them to a new folder and add a little Perl script, which I've called 'bulkresize.pl', and which I'll run from the command line when I need it.

First of all, I wanted to test to make sure it'd work as expected on a single image, so I wrote a single-image sub

use Image::Magick;

$image = Image::Magick->new();
sub resizeOne	{
	$image->Read('p100s.jpg');
	$image->Scale('40x40');
	$image->Write('jpeg:p100s2.jpg');
}
&resizeOne;

One quick test later and I'm ready to do the bulk resize

The steps are, basically:

loop through the directory
Resize each image as I find it
Here's the first version I whipped up

sub resizeDir	{
	for(`dir /B`)	{
		my $image = Image::Magick->new();
		chomp;
		if($_ =~ m/\.jpg/)	{
			$image->Read($_);
			$image->Scale('40x40');
			$image->Write("jpeg:$_");
			print("resized : $_ \n");
		}	else	{
			print "non-jpg\n";
		}
		$image = '';
	}
}

I use a backtick operation to get my directory listing and loop through it. I create a new ImageMagick object, use chomp to lop the end linebreak off the current filename. Next I check it's a JPEG, and if it is, I read the file, scale the file and then save the file with Write(). One thing to note, if you don't use a new Image::Magick object for each image, it'll have some not nice results, so do it this way.

This is rather simple, but there's a more elegant way which occurred to me after I'd resized all my images, which I used to get 20x20 icons. Here it is.

sub resizeDir	{
	for(`dir *.jpg /B`)	{
		my $image = Image::Magick->new();
		chomp;
		$image->Read($_);
		$image->Scale('20x20');
		$image->Write("jpeg:$_");
		print("resized : $_ \n");
		$image = '';
	}
}

Notice what I've done? Instead of grabbing the entire directory listing, I've only grabbed files which match the mask '*.jpg'. This means I don't need that conditional/regular expression to check I actually want to resize the file. This runs a bit quicker, a bit more smoothly, and of course it's more elegant looking.

Now, once you've used this, you'll probably start to think 'But ImageMagick can do more than just resizing'. That's right friends, it can do virtually any image operation you can think of, from applying transparency or text, to cropping, scaling, changing format, blurring, sharpening, solarizing and many, many other operations. I'll leave you to play with those for yourselves. Now, with the time you've saved by bulk editing your images, you can take that longer lunchbreak you've been hanging out for (tell your boss that you were running a large batch operation over lunch so couldn't use your machine for anything else if you get caught).

Jason Brown

 

ScrewTurn Wiki version 3.0.5.600. Some of the icons created by FamFamFam.