For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > February 2006 > Re: Manipulate graphics with Perl









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Manipulate graphics with Perl
Zentara

2006-02-21, 7:55 am

On Mon, 20 Feb 2006 15:12:21 -0500 (EST), scott@4way.us ("Scott") wrote:

>I need to be able to take an image and break it down in to smaller blocks.
>This would be used to create an HTML page of smaller images together
>displaying the large image as a whole, basically like the slice tool in
>Adobe. I was wondering if there was a way Perl could do this? If so can
>someone point me in the right direction.
>
>Thanks!
>Scott


As seen on RONCO-TV :-)
Here is a slicer-dicer that I wrote awhile back. Feed it an image, and
it will dice up the image and automatically makes an html file to show
to it. There are premade links to correspondinly named html files too.

Works best with jpegs

#!/usr/bin/perl
use warnings;
use strict;
use Imager;

my @tiles = ();
my $file = shift || die "need filename\n";
my $tempname = $file;
$tempname =~ s/^(.+)(\.\w+)$/$1/;
print "$tempname\n";

#set tile size adjustment
my $x = 100;
my $y = 100;

my $image = Imager->new();
$image->open(file=>$file, type=>'jpeg') or die $image->errstr();
my $width = $image->getwidth();
my $height = $image->getheight();

print "width->$width height->$height\n";

my $rows = int($height/$y +1) - 1; #make it 0 based
my $cols = int($width/$x + 1) - 1;
print "rows->$rows cols->$cols\n";

foreach my $row(0..$rows){
foreach my $col(0..$cols){
my $imageout = Imager->new(xsize=>$x, ysize=>$y, type=>'direct');


$imageout = $image->crop(left=>$col*$y, right=>$col*$y+$y,
top=>$row*$x, bottom=>$row*$x+$x);

my $tilename = $tempname .'-'.$row.'-'.$col.'.jpg';

$imageout->write(file=>$tilename, type=>'jpeg')
or die "Cannot write: ",$imageout->errstr;


}
}

########################################
##############################
#make an html page with tiles reassembled and clickable

open(HT,">$tempname-jpg.html") or warn $!;

print HT "<html><body><h1>$tempname.jpg</h1>";
print HT "<table border=0 cellspacing=0 cellpadding=0>";

#putting border=0 in the IMG<> makes it seamless
#I have border=1 for demonstration
foreach my $row(0..$rows){
print HT "<tr>";
foreach my $col(0..$cols){
print HT "<td><a href= $tempname-$row-$col.html>
<IMG SRC=$tempname-$row-$col.jpg border=1 HEIGHT=$y
WIDTH=$x
alt=$tempname-$row-$col.jpg></a></td>"
}
print HT "</tr>";
}
print HT "</table></body></html>";

__END__


--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com