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

I would want to get some pixel color of the X screen the perl application is currently running on.

Im using perl-TK to generate the user interface, so maybe theres something useable there. The pixel I want to "read" is outside the user interface.

But is it possible to get the pixel color of a specific pixel, and hopefully without taking a screenshot and then using GD/ImageMagick because that would be pretty memory/CPU hungry.

Something like:

($r,$g,$b) = [somemodule]->pixel($x,$y);

Replies are listed 'Best First'.
Re: Getting pixel color of screen
by zentara (Cardinal) on Dec 14, 2011 at 11:03 UTC
Re: Getting pixel color of screen
by Anonymous Monk on Dec 14, 2011 at 04:08 UTC

    No, any way you slice it, you're taking a screenshot, but AFAIK, Tk exposes no API to retrieve said information.

    http://search.cpan.org/grep?cpanid=SREZIC&release=Tk-804.030&string=pixel&i=1&n=1&C=0

    update: well, it might be possible with Tk::WinPhoto on linux, but it doesn't expose

    GetDesktopWindow http://search.cpan.org/grep?cpanid=SREZIC&release=Tk-804.030&string=desktop&i=1&n=1&C=0

    So at the very least you'd need X11::GuiTest

    Probably easiest to use Imager::Screenshot

    See also Gtk2-screenshot, Tk Screen and Canvas Screenshots

Re: Getting pixel color of screen
by Khen1950fx (Canon) on Dec 15, 2011 at 06:50 UTC

    This isn't a specific answer, but maybe it might give you some idea on which direction to go. Read the documentation for

    Imager
    Imager::ImageTypes
    Imager::Draw

    and then try this:
    #!/usr/bin/perl -l use strict; use warnings; use Imager; use Imager::Fill; use Data::Dumper::Concise; my $file = 'Screenshot.png'; my $img = Imager->new(); $img->open( file => $file, type => 'png' ); print "Image information: \n"; print "Width: ", $img->getwidth(); print "Height: ", $img->getheight(); print "Channels: ", $img->getchannels(); print "Bits/Channel: ", $img->bits(); print "Virtual: ", $img->virtual() ? "Yes" : "No"; my $colorcount = $img->getcolorcount; print "Actual number of colors in image: "; print defined($colorcount) ? $colorcount : ">512"; print "Type: ", $img->type(); if ($img->type eq 'direct' ) { print "Modifiable Channels: "; print join " ", map { ($img->getmask() & 1<<$_) ? $_ : () } 0..$img->getchannels(); } my(@rgbanames) = qw( red green blue alpha ); my $mask = $img->getmask(); for (0..$img->getchannels() - 1) { print $rgbanames[$_] if $mask & 1<<$_; } my $color = $img->getpixel(x=>50, y=>70); print Dumper($color);