GD.pm

abez abez at abez.ca
Thu Jan 23 22:37:12 CST 2003


Wow!

Previously I've strung things together using pipes and the netpbm packages so
I could do things like

png2pnm $file | pnmscale -width 640 -height 480 | cjpeg > $file.jpg

So this got slow, tedious and tired.

I then downloaded the latest version of Gd installed the library.

then I went and got CPAN to install GD for Perl.

Then I made this script:

#!/usr/bin/perl
use GD;
use strict;
my $WIDTH = 640;
my $HEIGHT = 480;
foreach my $file (<*.jpg>) {
	print "Converting $file\n";
	my $image = GD::Image->newFromJpeg($file,1);
	my ($iw,$ih) = $image->getBounds();
	if ($ih > $iw) {
		print "\tRotating $file\n";
		$image = $image->copyRotate90();
		($iw,$ih) = $image->getBounds();
	}
	my $imageout = GD::Image->new($WIDTH,$HEIGHT,my $truecolor=1);
	$imageout->copyResized($image,my $dstX=0,my $dstY=0,my $srcX=0,
                               my $srcY=0,$WIDTH,$HEIGHT,$iw,$ih);
	print "\tWriting $file\n";
	open(FILE,">./tmp/".$file) or next;
	binmode FILE;
	print FILE $imageout->jpeg;
	close(FILE);
}

I load all the jpegs in a directory, if they are taller than they are wide I
rotate them 90 degrees, then I resize the image down to 640x480 (no I don't
need to maintain aspect ratio). and write it out to a jpeg.

So this script parsed about 80,000 images in about 4 hours. 20000 images an
 hour. so about 5 per second. That was pretty good. Oh and this was running in
the background at the lowest priority so it wasn't the most important job.

I'm just really impressed I can write a script in 10 minutes using a library
just ten minutes after downloading the thing. These GD libs are really neat..
I doubt I'll use netpbm much anymore.

-- 
abez----- ----- ------ - ------ -- ------------
http://www.abez.ca/ Abram Hindle (abez at abez.ca)
--- --- ------ --------- - - ------ --------abez



More information about the Victoria-pm mailing list