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

Hello ! I need to read a pixel color from 3d app at coordinates. I can't do that, and everything i found in google is about images. This is a live 3d app and i want to perl look on specific coordinates and found color change, and then do something like example in egnlish if color is black at coordinates x, y then keystroke. I understand if,elsif, else, and i know how to do keystrokes, but i can't tell my program to read a pixel color from application (the top one), i can't tell him to read any color even from desktop. At this moment using windows 7 + strawberry. Any ideas how to get it ?
  • Comment on How to get pixel color from 3d application

Replies are listed 'Best First'.
Re: How to get pixel color from 3d application
by BrowserUk (Patriarch) on Feb 28, 2016 at 20:31 UTC

    Unless the application has an api for doing this, and you have access to a perl-callable library to use that api, the only way I could see to do this would be to do a screen-grab and then inspect the resultant bitmap.

    Your problem then would be converting between the coordinate system used within the application and the screen coordinates represented in the bitmap; and that would be distinctly non-trivial.

    You might also be able to use something like Pixie to obtain the color of the pixel under the cursor, but automating that would probably be quite difficult; and you still have the coordinate convertion problem to resolve.


    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.
Re: How to get pixel color from 3d application
by Old_Gray_Bear (Bishop) on Feb 28, 2016 at 20:41 UTC
    It would help us to help you if we knew the name of the "3d app at coordinates" that you are using. There may be an API that will get you going, but without the name of the application, we would be just guessing in the dark.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: How to get pixel color from 3d application
by Laurent_R (Canon) on Feb 28, 2016 at 21:19 UTC
Re: How to get pixel color from 3d application
by thomas895 (Deacon) on Feb 28, 2016 at 19:50 UTC

    how would you do this without perl? does the application have some sort of API you can use?

    -Thomas
    "Excuse me for butting in, but I'm interrupt-driven..."
Re: How to get pixel color from 3d application
by fishmonger (Chaplain) on Feb 29, 2016 at 16:29 UTC

    perl is my favorite language, but it's not always the right choice for a given task. There may well be a perl module available to help you with this task, but choosing another language might be best for this task. AutoHotkey Is the primary language used by gamers to automate or manipulate the game while playing it. I have never used it myself but all of the gamers in my dept love it.

Re: How to get pixel color from 3d application
by JuR (Initiate) on Feb 29, 2016 at 16:58 UTC
    30 second search how to make it in c. I know many apps like autohotkey. Anyway i want to make it by my own, why ? because this is the best way to learn. It's boring to make pointless scalars, arrays, readin files etc. When i trying to get something i have motivation, and i read alot and spend time to get it. I think perl can do much more than simple files tasks.
      How to capture screen under Win32?

      Not sure if you then want to process the data raw (BMP format is very easy to read) or use libraries like https://metacpan.org/pod/Imager

      Using Imager getpixel

      if raw and you want to read the pixels yourself, read: The simple explanation first: http://paulbourke.net/dataformats/bmp/

      Now the more complex explanation: https://en.wikipedia.org/wiki/BMP_file_format

      I could not find the exact post, but I know it exists on PM, it was for BMP with pack. This should do as a start: BMP file manipulation

      Just know that with AutoIt3 you can do all this with 1 command: https://www.autoitscript.com/autoit3/docs/functions/PixelGetColor.htm

Re: How to get pixel color from 3d application
by JuR (Initiate) on Feb 28, 2016 at 21:58 UTC
    aplication is .exe ofc. Actually this is a game, and i want to start with private server of this game, and i want to add a few things. Aplication dosent have any api. perl have to read just one pixel from one coordinates (x,y) for a color change, and then do his code. Let's say it's a bar in a corner, bar which change color, and perl have to know which color is actually in use to do his code. it should be easy to do, but i dont know yet how to get that works :). just first few days in perl. Sorry Laurent_R for crossing post. i just tryin to resolve this problem asap. Thanks for reply cheers. Together we will resolve this problem :)
Re: How to get pixel color from 3d application
by JuR (Initiate) on Mar 01, 2016 at 17:49 UTC
    Hi it's me again :P I found a way how to get that working. at this moment code is very primitive, but it do what i want. ofc ill post the code below, because maybe someone may need it, and will modify it by himself. This is my first week with perl (and i don't know any other programming language). I have to say it again it's VERY PRIMITIVE JUST FOR TEST. GOTO made code slower. Just put it here, to check if it's works.
    use Imager; use Imager::Screenshot 'screenshot'; use Win32::GuiTest qw(FindWindowLike GetWindowText SetForegroundWindow SendKeys); START: my $img = screenshot(); #make screenshot $img->write(file => 'screenshot.png', type => 'png' ) || print "Failed: ", $img->{ERRSTR} , "\n"; #write a new sreenshot o +ver old one #or send error $img=Imager->new(); #make new image $img->read(file=>'screenshot.png',type=>'png'); #read from fresh scre +enshot $color = $img->getpixel(x=>68, y=>58); #get pixel from coord +inate x,y $img1=Imager->new(); #make new image $img1->read(file=>'orginal.png',type=>'png'); #1.read from orginal +pcitrue i have made $color1 = $img1->getpixel(x=>68, y=>58); #2. before to compar +e element from $keys = (22); #3. the same coordina +tes, so when #4. window is change +on any diferent color #5. program show doin + code (else) if ($color->equals(other=>$color1)) { #if color is the same print "ok\n" #print ok, do nothing. } else { #else press key 22 from $key +s SendKeys ($keys, 100) } sleep 1; #sleep for one sec goto START; #go back and do compare aga +in with new screenshot

      You can improve performance by only fetching a subset of the screen with screenshot:

      my $img = screenshot(left => 68, right => 69, top => 58, bottom => 59);

      You're also writing the screenshot to a file, then reading the same image back in again, which is a waste.

        Good idea, ill try to modify the code, thanks. If someone have a suggestions, please post it :) Thanks !