wiredfool

Updated Imagemagick Thumbnail script

A few years ago, I posted a script that I used for generating image thumbnails. I’ve changed my usage of it, so I’m posting another go of it. Since then, OSX has shipped, the Imagemagick .pkg distribution comes with the perl bindings, and I’ve figured out that I generally just want three sizes and 90 degree rotations for the images.

So I generate three sizes into a seperate directory from (usually) 1600px max size images: a 175px (thumbnail, -tm), 400px (medium, -md) and 800px (penultimate, -pt). There’s also a straight 90 degree rotation for each size that gets a -r in the filename. There are also a few index.html files for that quick web based overview.

Call the script using perl thumbnail.pl [directory]

This script assumes that your source images are in ~/Pictures/[directory]/, and that the output should be put in ~/Sites/thumbs/[directory]/. Edit the paths if you want them in different locations.

#! /usr/bin/perl
 
use Image::Magick;
 
$targetExtension = "(jpg)|(tif)|(bmp)|(JPG)";
$thumbnailExtension = "jpg";
$thumbnailSize = "175x175";
$mediumSize = "400x400";
$thumbnailName = "-tm";
$pentSize="800x800";
$mediumName = "-md";
$lgName = "-lg";
$pentName = "-pt";
 
($startDir, @rest) = @ARGV;
 
my $image;
 
$destDir = $ENV{HOME}."/Sites/thumbs/".$startDir;
 
if  (!( $startDir =~ /\//)){
    $startDir = $ENV{HOME}."/Pictures/".$startDir;
    print $startDir, "\n";
}
 
opendir (START, $startDir) || die "Couldn't Open Start dir: $startDir";
 
@files = readdir(START);
 
closedir (START);
 
if (not -d $destDir) {
        mkdir ($destDir, 0775);
}
 
open (OUTFILE, ">$destDir/index.html") or die "Couldn't open index.html";
open (OUTFILE2, ">$destDir/index2.html") or die "Couldn't open index2.html";
open (OUTFILE3, ">$destDir/index3.html") or die "Couldn't open index3.html";
 
foreach $file (sort @files) {
        ($fname,$extension) = split(/\./,$file);
        $fname =~ s/$lgName//;
        if ($extension =~ /$targetExtension/i) {
                print "$fname \n";
                $image = new Image::Magick;
                $image->Read("$startDir/$file");
                $image->Scale(geometry=>$pentSize);
                $image->Write("$destDir/$fname$pentName.$thumbnailExtension");
                $image->Rotate(degrees=>90);
                $image->Write("$destDir/$fname$pentName-r.$thumbnailExtension");
 
                $image->Scale(geometry=>$mediumSize);
                $image->Write("$destDir/$fname$mediumName-r.$thumbnailExtension");
                $image->Rotate(degrees=>270);
                $image->Write("$destDir/$fname$mediumName.$thumbnailExtension");
 
                $image->Scale(geometry=>$thumbnailSize);
                $image->Write("$destDir/$fname$thumbnailName.$thumbnailExtension");
                $image->Rotate(degrees=>90);
                $image->Write("$destDir/$fname$thumbnailName-r.$thumbnailExtension");
 
        print OUTFILE "\n";
        print OUTFILE2 "\n";
        print OUTFILE3 "< /a>\n";
        }
}

close OUTFILE;
close OUTFILE2;
close OUTFILE3;
No comments

No comments yet. Be the first.

Leave a reply

You must be logged in to post a comment.