LloydRice has asked for the wisdom of the Perl Monks concerning the following question:
So far, so good. It is said that I can also use the getpixel invocation directly as the method reference to run rgba().c:>type gp1.pl use Imager; my $image_source = "\\c\\perl\\graphics\\" . shift . ".bmp"; my $image = Imager->new; $image->read(file=>$image_source) or die "Cannot load $image: ", $image->errstr; $width = $image->getwidth(); $height = $image->getheight(); print "Image dimensions: height = $height, width = $width\n"; $x = 10; for $y ( 10 .. 15 ) { $color = $image->getpixel( x=>$x, y=>$y, type=>'8bit' ); ( $r, $g, $b, $a ) = $color->rgba(); print " shade = $r, $g, $b\n"; } c:>gp1 d2044 Image dimensions: height = 348, width = 349 shade = 238, 238, 238 shade = 255, 255, 255 shade = 255, 255, 255 shade = 238, 238, 238 shade = 238, 238, 238 shade = 238, 238, 238 c:>
Very good. But now it seems like a lot of work to call getpixel separately for each pixel. The 'draw' tutorial says that I can pass a reference to a list of pixel addresses to getpixel(). I should get back a list of references to a color method. I tried it this way.c:>type gp2.pl use Imager; my $image_source = "\\c\\perl\\graphics\\" . shift . ".bmp"; my $image = Imager->new; $image->read(file=>$image_source) or die "Cannot load $image: ", $image->errstr; $width = $image->getwidth(); $height = $image->getheight(); print "Image dimensions: height = $height, width = $width\n"; $x = 10; for $y ( 10 .. 15 ) { ( $r, $g, $b, $a ) = $image->getpixel( x=>$x, y=>$y, type=>'8bit' )->rgba(); print " shade = $r, $g, $b\n"; } c:>gp2 d2044 Image dimensions: height = 348, width = 349 shade = 238, 238, 238 shade = 255, 255, 255 shade = 255, 255, 255 shade = 238, 238, 238 shade = 238, 238, 238 shade = 238, 238, 238
No luck. It seems that the elements of @colors are undefined. I presume something is wrong with the getpixel() call. OK. So maybe if one axis is a reference, then both have to be.c:>type gp3.pl use Imager; my $image_source = "\\c\\perl\\graphics\\" . shift . ".bmp"; my $image = Imager->new; $image->read(file=>$image_source) or die "Cannot load $image: ", $image->errstr; $width = $image->getwidth(); $height = $image->getheight(); print "Image dimensions: height = $height, width = $width\n"; $x = 10; $y = [ 10 .. 15 ]; @colors = $image->getpixel( x=>$x, y=>$y, type=>'8bit' ); for $color ( @colors ) { ( $r, $g, $b, $a ) = $color->rgba(); print " shade = $r, $g, $b\n"; } c:>gp3 d2044 Image dimensions: height = 348, width = 349 Can't call method "rgba" on an undefined value at gp3.pl line 15.
Still doesn't work. What am I missing?c:>type gp4.pl use Imager; my $image_source = "\\c\\perl\\graphics\\" . shift . ".bmp"; my $image = Imager->new; $image->read(file=>$image_source) or die "Cannot load $image: ", $image->errstr; $width = $image->getwidth(); $height = $image->getheight(); print "Image dimensions: height = $height, width = $width\n"; $x = [ 10 ]; $y = [ 10 .. 15 ]; @colors = $image->getpixel( x=>$x, y=>$y, type=>'8bit' ); for $color ( @colors ) { ( $r, $g, $b, $a ) = $color->rgba(); print " shade = $r, $g, $b\n"; } c:>gp4 d2044 Image dimensions: height = 348, width = 349 Can't call method "rgba" on an undefined value at gp4.pl line 15.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Using Imager getpixel
by Khen1950fx (Canon) on Dec 30, 2011 at 11:04 UTC | |
by LloydRice (Beadle) on Dec 31, 2011 at 08:52 UTC | |
by tonyc (Friar) on Jan 03, 2012 at 04:42 UTC | |
by Anonymous Monk on Sep 22, 2014 at 17:16 UTC | |
Re: Using Imager getpixel
by BrowserUk (Patriarch) on Dec 30, 2011 at 09:38 UTC |