#!/usr/bin/perl -w use strict; # ----------- # 'split' written by David Wakeman # and distributed using the GPL license. # # Created 03.28.2002 on planet earth. # # Use at your own risk. I am not responsible # for any loss of data, loss of life, computer # equipment explosion, divorce, dropped food # or anything else that you may think happened # because of this program. # # Enjoy! # # djw@perldev.org # http://perldev.org/ # ----------- use Imager; use IO::File; use Getopt::Long; use File::Spec::Win32; use File::DosGlob 'glob'; use Time::HiRes qw( gettimeofday ); # ----------- # command-line args (html on|off and image list) my ($hoff, @images); GetOptions( "hoff" => \$hoff ); if (@ARGV) { foreach (@ARGV) { push(@images, $_); } } else { @images = glob "*.jpg"; } my $imageCount = @images; unless ($imageCount > 0) { print "No images found!\n\n"; &usage; } &process; # ----------- # main sub sub process { my $count = 0; my $message = "Cannot crop image"; open(LOG, "+>splitlog.txt") || die "Can't open log: ($!)\n"; my ($secs, $msecs) = gettimeofday; foreach (@images) { $_ =~ /(.*?).jpg/i; my $pic = $1; # ----------- # start our image object my $img = Imager->new(); $img->open(file => "$_"); # ----------- # check image size my $imgHeight = $img->getheight(); my $imgWidth = $img->getwidth(); if ($imgHeight != 300 && $imgWidth != 654) { print LOG "$_ does not conform to x,y requirements - skipped.\n"; next; } # image is good so increment $count++; # ----------- # crop image 6 times my $newImage01 = $img->crop( left=>0 , right=>218, top=>0 , bottom=>150 ) || die "$message: ($!)\n"; my $newImage02 = $img->crop( left=>218, right=>437, top=>0 , bottom=>150 ) || die "$message ($)): ($!)\n"; my $newImage03 = $img->crop( left=>437, right=>654, top=>0 , bottom=>150 ) || die "$message ($_): ($!)\n"; my $newImage04 = $img->crop( left=>0 , right=>218, top=>150, bottom=>300 ) || die "$message ($_): ($!)\n"; my $newImage05 = $img->crop( left=>218, right=>437, top=>150, bottom=>300 ) || die "$message ($_): ($!)\n"; my $newImage06 = $img->crop( left=>437, right=>654, top=>150, bottom=>300 ) || die "$message ($_): ($!)\n"; # ----------- # write it out $newImage01->write(file => "$pic-01.jpg"); $newImage02->write(file => "$pic-02.jpg"); $newImage03->write(file => "$pic-03.jpg"); $newImage04->write(file => "$pic-04.jpg"); $newImage05->write(file => "$pic-05.jpg"); $newImage06->write(file => "$pic-06.jpg"); unless ($hoff) { &writeHTML($pic, $_); } } my $createCount = $count * 6; my ($secs2, $msecs2) = gettimeofday; my $stime = $secs2 - $secs; my $mtime = $msecs2 - $msecs; my $totalTime = "$stime.$mtime"; print LOG "Files processed: $count\n"; print LOG "Images created : $createCount\n"; print LOG "Processing time: $totalTime seconds\n"; close(LOG); } # ----------- # on by default sub writeHTML { my ($pic, $originalImage) = @_; my (%imageInfo, $kbytes, $sum); ($kbytes) = (stat($originalImage))[7]; my $origSize = $kbytes / 1024; $origSize =~ /(\d+)\.(\d\d)/; my $originalImageSize = "$1.$2"; my @newImages = ( "$pic-01.jpg", "$pic-02.jpg", "$pic-03.jpg", "$pic-04.jpg", "$pic-05.jpg", "$pic-06.jpg", ); foreach (@newImages) { ($kbytes) = (stat($_))[7]; my $size = $kbytes / 1024; $size =~ /(\d+)\.(\d\d)/; $imageInfo{$_} = "$1.$2"; $sum += "$1.$2"; } open(FILE, "+>$pic.html") || die "Can't create html file: ($!)\n"; print FILE "$pic.jpg preview\n"; print FILE "\n"; # ----------- # css style tags print FILE "\n"; # ----------- # html meat print FILE "\n"; print FILE "\n"; print FILE "
\n"; print FILE "\n"; print FILE "\n"; print FILE " \n"; print FILE "\n"; print FILE "\n"; print FILE "\n"; print FILE "
File Information
\n"; print FILE "Original size: $originalImageSize KB
\n"; print FILE "Combined size: $sum KB\n"; print FILE "
    \n"; foreach (@newImages) { print FILE "$_: $imageInfo{$_} KB
    \n"; } print FILE "
\n
\n"; # ----------- # image table print FILE "\n"; print FILE "\n"; # ----------- # first set of images across (row 1) print FILE " \n"; print FILE " \n"; print FILE " \n"; print FILE "\n"; # ----------- # second set of images across (row 2) print FILE " \n"; print FILE " \n"; print FILE " \n"; print FILE "
\n"; print FILE "
\n"; print FILE "\n"; print FILE "\n"; close(FILE); } # ----------- # called by command-line option # or if it can't find any jpg's # in current dir. sub usage { print "\nusage: split.pl -hoff [ [filename 1] [filename 2] ]\n\n"; print " By default split.pl with no arguments will attempt to\n"; print " process every jpg file in the current directory, and will\n"; print " produce an html page per image 'set' so you can view output\n"; print " and see file size information.\n\n"; print "\t'split.pl foo.jpg bar.jpg foobar.jpg'\n"; print "\t\tor\n"; print "\t'split.pl -hoff foo.jpg bar.jpg foobar.jpg'\n\n"; print " The first method allows you to specify certain images and\n"; print " the second turns off html output and specifies only certain\n"; print " images to be processed. The html off option can be called\n"; print " by itself.\n\n"; print " Please read the readme.txt associated with this program for\n"; print " more info.\n"; exit; }