Category: | Web Stuff |
Author/Contact Info | djw |
Description: | Split has one purpose: crop 654x300 images into 6 equal sized smaller images that span two rows, and three columns. Html output (one page per image) is optional (on by default).
I decided to do this after I talked to a buddy of mine Chris (from the sexy http://soulinjection.com/) about a site called http://www.altsense.net/. We were looking at some of the very cool art in the photo albums and saw that some people cut up a single larger picture into 6 pieces so they could fit the entire thing into one page of the album (you will have to go check out the site to see what I mean). Chris was telling me that this process can take a long time, and I mentioned I could write something to automate it. TaDA! Split was created. This program was written specifically for the altsense.net image gallery, but it could be expanded for your own use if you feel like it. Or maybe you just need a chunk of code from it for something you are doing. Thought I'd post it anyhow. Sample: my car! Thanks, djw |
#!/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 - skipp +ed.\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 "<html><head><title>$pic.jpg preview</title>\n"; print FILE "<!-- \'split\' written by David Wakeman - http://perld +ev.org/ -->\n"; # ----------- # css style tags print FILE "<style type=\"text/css\">\n"; print FILE "\tBODY {\n"; print FILE "\t\tbackground: #FFFFFF;\n"; print FILE "\t}\n"; print FILE "\t.imageframe {\n"; #print FILE "\t\tborder: 1px solid black;\n"; print FILE "\t}\n"; print FILE "\t.titleinfo {\n"; print FILE "\t\tborder: 1px solid black;\n"; print FILE "\t\tcolor: #FF6600;\n"; print FILE "\t\tfont-family: Tahoma, Verdana, sans-serif;\n"; print FILE "\t\tfont-size: 10px;\n"; print FILE "\t\tfont-weight: normal;\n"; print FILE "\t\ttext-decoration: none;\n"; print FILE "\t}\n"; print FILE "</style>\n"; # ----------- # html meat print FILE "</head>\n"; print FILE "<body>\n"; print FILE "<center>\n"; print FILE "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\ +" width=\"200\">\n"; print FILE "<tr>\n"; print FILE " <td class=\"titleinfo\" align=\"center\"><b>File Info +rmation</b></td>\n"; print FILE "</tr>\n"; print FILE "<tr>\n"; print FILE "<td class=\"titleinfo\">\n"; print FILE "<b>Original size</b>: $originalImageSize KB<br>\n"; print FILE "<b>Combined size</b>: $sum KB\n"; print FILE "<ul>\n"; foreach (@newImages) { print FILE "<b>$_</b>: $imageInfo{$_} KB <br>\n"; } print FILE "</ul></td></tr>\n"; print FILE "</table>\n<br>\n"; # ----------- # image table print FILE "<table border=\"0\" cellpadding=\"0\" cellspacing=\"4\ +">\n"; print FILE "<tr>\n"; # ----------- # first set of images across (row 1) print FILE " <td class=\"imageframe\"><img src=\"$pic-01.jpg\" bor +der=\"0\"></td>\n"; print FILE " <td class=\"imageframe\"><img src=\"$pic-02.jpg\" bor +der=\"0\"></td>\n"; print FILE " <td class=\"imageframe\"><img src=\"$pic-03.jpg\" bor +der=\"0\"></td>\n"; print FILE "</tr><tr>\n"; # ----------- # second set of images across (row 2) print FILE " <td class=\"imageframe\"><img src=\"$pic-04.jpg\" bor +der=\"0\"></td>\n"; print FILE " <td class=\"imageframe\"><img src=\"$pic-05.jpg\" bor +der=\"0\"></td>\n"; print FILE " <td class=\"imageframe\"><img src=\"$pic-06.jpg\" bor +der=\"0\"></td>\n"; print FILE "</tr></table>\n"; print FILE "</center>\n"; print FILE "</body>\n"; print FILE "</html>\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 outpu +t\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 certai +n\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 fo +r\n"; print " more info.\n"; exit; } |
|
---|
Replies are listed 'Best First'. | |
---|---|
•Re: altsense.net image splitter
by merlyn (Sage) on Mar 30, 2002 at 17:30 UTC | |
by djw (Vicar) on Apr 01, 2002 at 15:00 UTC | |
by merlyn (Sage) on Apr 01, 2002 at 17:52 UTC |