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

I am currently using Win32::GuiTest to try and automate a series of screen captures, but I need to do some analysis of each captured image before deleting them.

Presently, I've resorted to saving them to disk and then parsing the resulting BMP back into memory before pondering the pixel grid:

sub grabWindowRegion { my $ds = new Win32::GuiTest::DibSect; $ds->CopyClient(shift,[@_]); $ds->SaveAs('temp.bmp'); return parseBMP('temp.bmp'); }
Writing to disk just to immediately read it back is really silly and wasteful, but I needed to just get it done.

The DibSect object is opaque ($VAR1 = bless( do{\(my $o = 28375616)}, 'Win32::GuiTest::DibSect' );) to naive Dumpering, and I'm not seeing any perl source for it in the install directories.

In the end, I just want a 2D grid of pixel colors in an AoA ref or equivalent. At a minimum, the ability to peek at pixel values in the image.
How should I be doing this?

Current Solution:

  1. SendKeys('^{PRTSCN}');
  2. my $image = Win32::Clipboard::GetBitmap();
  3. Crop separately

Replies are listed 'Best First'.
Re: Win32::GuiTest - Processing screen captures in memory?
by BrowserUk (Patriarch) on Mar 19, 2011 at 20:22 UTC

    Rather than saving it to a file, copy it to the clipboard and then retrieve it from there using Win32::ClipBoard:

    sub grabWindowRegion { my $cb = new Win32::ClipBoard; my $ds = new Win32::GuiTest::DibSect; $ds->CopyClient(shift,[@_]); $ds->ToClipboard(); my $image = Win32::Clipboard::GetBitmap(); return parseBMP( $image ); }

    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.

      I'm not sure what's wrong with that... the image does end up on the clipboard, but I can't get it back out into $image.

      grabWindowRegion(0, 0,0,64,64); sub grabWindowRegion { my $ds = new Win32::GuiTest::DibSect; $ds->CopyClient(shift,[@_]); $ds->ToClipboard(); print Win32::Clipboard::IsBitmap() ? "Bitmap\n" : "Not Bitmap\n"; my $image = Win32::Clipboard::GetBitmap(); #Same results with just Get() print Dumper $image; die "\n"; # $ds->SaveAs('temp.bmp'); # return parseBMP('temp.bmp'); }
      Gives:
      Not Bitmap $VAR1 = '';

      The image is definitely on the clipboard though, since if I open mspaint, I can Ctrl-V and get the "my computer" icon perl captured from my desktop...

        You are calling GetBitmap as a function. The documentation shows it being called as a class method. I suspect that is the cause of your problem as the following snippet successfully retrieves, saves and displays any image I place on the clipboard:

        #! perl -slw use strict; use Win32::Clipboard; my $cb = Win32::Clipboard->new(); my $bmp = $cb->GetBitmap; open O, '>:raw', 'junk.bmp' or die $!; print O $bmp; close O; system 'junk.bmp';

        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.
        IIRC, clipboard/bitmap is 24-bit type bitmap ... can your parseBMP deal with it?