I have a question about the Imager package. This is ActiveState v5.12.4 with Win7Pro on a 64-bit Gateway SX-2841. I would like to access the pixel values from a BMP image.
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:>
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 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
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 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.
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 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.
Still doesn't work. What am I missing?

In reply to Using Imager getpixel by LloydRice

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.