Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Extracting pixel values

by redbeard (Beadle)
on Mar 26, 2007 at 23:14 UTC ( [id://606685]=perlquestion: print w/replies, xml ) Need Help??

redbeard has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I am working on a project to extract wave height data from about ten years of hourly images. Across the image, I can calculate the the pixel (x and y) of the site I want to extract data from. The pixel at that site has a color that corresponds to a standard wave height value.

My question is, how do I extract that pixel color? I've seen lots of code flying around for filling colors with a certain pixel color, but not so much on extracting information from a pre-existing image.

Any pointers? Would something in GD or other module work?

Replies are listed 'Best First'.
Re: Extracting pixel values
by BrowserUk (Patriarch) on Mar 26, 2007 at 23:19 UTC

    If the image is in one of the forms supported by GD, then once you have loaded the image in the usual way, then you can use getPixel( x, y ) to obtain the color value. From the docs:

    $index = $image->getPixel(x,y) object method This returns the color table index underneath the specified point. It can be combined with rgb() to obtain the rgb color underneath the p +ixel. Example: $index = $myImage->getPixel(20,100); ($r,$g,$b) = $myImage->rgb($index);

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Extracting pixel values
by Joost (Canon) on Mar 26, 2007 at 23:18 UTC
      In GD - Is there a way to *retrieve* the alpha value of a pixel? I can do $i = $img->getPixel($x,$y); and use rgb() to get its rgb value, and the perldoc says something about hexadecimal strings carrying it. But I can't see a way to extract the hex-string. Everything seems to be concentrated on drawing, rather than extracting values from pictures.
Re: Extracting pixel values
by merlyn (Sage) on Mar 27, 2007 at 01:14 UTC
Re: Extracting pixel values
by duff (Parson) on Mar 27, 2007 at 03:19 UTC

    You've been given a few excellent answers to your question already, but I'd like to suggest that if you're manipulating images, you might want to do so in a vector sort of way. Check out PDL from CPAN or the PDL home page if you haven't already.

Re: Extracting pixel values
by dk (Chaplain) on Mar 27, 2007 at 14:35 UTC
    Unless you have your image data already available as some internal format in memory, and pixel extractions are relatively infrequent, then possibly you'd be just fine with plain substr. For example, for a 8-bit non-aligned image:

    sub get_pixel($$) { my ( $x, $y) = @_; substr ( $image, $y * $height + $x, 1); }

    I know this is over-simplification, but I use these direct memory hacks once in a while, f.ex. $image = ~$image for images of cardinal pixels is as valid and fast as any other image negation. Your pixel extraction problem might just as well hit the same area.

    Otherwise, of course, a number of libraries provide generic image access, as mentioned above.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://606685]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found