Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Getting pixel colours with Image::Magick

by Cody Fendant (Friar)
on Aug 11, 2019 at 08:28 UTC ( #11104276=perlquestion: print w/replies, xml ) Need Help??

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

I'm using Image::Magick to get the individual colour of each pixel of an image.

What I'm getting back when I do this:

GetPixel( 'x' => $x_position, 'y' => $y_position );

Is something like this:

[ '0.980392156862745', '0.996078431372549', '0.996078431372549' ]

And I'm having to turn that into a hexadecimal code myself with a subroutine where each value is translated with

sprintf( "%02x", $_ * 255 );

So, is there any way I can get back hex values from GetPixel?

I'm a little confused about where full documentation for the perl ImageMagick module could be found. The stuff on CPAN is quite brief (doesn't even mention GetPixel), and the stuff on the ImageMagick site doesn't really help.

Replies are listed 'Best First'.
Re: Getting pixel colours with Image::Magick
by vr (Deacon) on Aug 11, 2019 at 10:45 UTC

    You could add 'normalize => 0' arguments to the GetPixel call, and then get integers, most probably in 0 .. 0xFFFF range, because QuantumDepth of modern ImageMagick is 16 bit per channel by default. Even in the past, when it was 8 bit, you'd get integers 0 .. 0xFF, and certainly not strings in hexadecimal representation -- so basic arithmetic/formatting operation would be required for your purposes anyway.

Re: Getting pixel colours with Image::Magick
by LanX (Archbishop) on Aug 11, 2019 at 09:05 UTC
    > I'm a little confused about where full documentation for the perl ImageMagick module could be found.

    The module Image::Magick is a thin wrapper around ImageMagick the binary which is normally a PITA to work with.

    Be warned: It has a very bad reputation about documentation, consistency and backwards compatibility.

    Most people I know who worked with it - including me - were swearing.

    You may find the correct features by trial and error, but don't be too sure your stuff works after updating IM.

    Sorry.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    Update

    Fixed typo

      Maybe you can suggest an alternative approach that you used successfully?

      Personally, I find Imager to work quite well. See Imager::Draw and Imager::Color:

      my @colors = $img->getpixel(x => 0, y => [ 0 .. 3 ], type => '8bit +'); # ... $img->setpixel(x=>50, y=>70, color=>[ 255, 0, 255 ]);

        The fork GraphicsMagick is supposed to be a safer, better version of ImageMagick. I expect the API and lack of docs will be similar though. I suppose a loose analogy would be GraphicsMagick is to ImageMagick as MariaDB is to MySQL.

        I was addressing the question why it's sparsely documented.

        Simple things work considerably well, but care should be taken.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11104276]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2019-08-23 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?