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

Here's the code I'm using; the output is a scalar quantity.

Is there a way with Imager module to convert the scalar quantity to rgba/hex colors?

#!/usr/bin/perl use Imager; # read file $img = Imager->new(); $img->read(file=>'pixelateMe.png', type=>'png') or die $img->errstr(); # create array - values for pixel values my @colors = $img->getpixel(x => [ 0 ], y => [ 0 .. 1 ]); print @colors[0]; # output is "scalar": Imager::Color=SCALAR(0xb97800)
  • Comment on Using module Imager, how to convert scalar-color value to hex/rgba color value?
  • Download Code

Replies are listed 'Best First'.
Re: Using module Imager, how to convert scalar-color value to hex/rgba color value?
by BrowserUk (Patriarch) on Dec 02, 2015 at 17:20 UTC

    Use the rgba method to get the individual values and printf to format them in hex:

    printf "%02x%02x%02x%02x\n", $colors[0]->rgba;

    To output the whole array:

    printf "%02x%02x%02x%02x\n", $_->rgba for @colors;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
    /div
Re: Using module Imager, how to convert scalar-color value to hex/rgba color value?
by tonyc (Hermit) on Jan 06, 2016 at 03:27 UTC

    I realize this is late, but if you want to go pixel by pixel over an image, you're much better off using getscanline() or getsamples()

    These fetch a partial or complete row from the image, which might not be suitable for your case (you're fetching columns), but might be for others who see this node.