Jump to content

Part of Screen to array


Recommended Posts

I'm new to autoIt, and last week I've build some simple scripts.

At the moment I want to make a program to interact with event's which happen on screen. I'm looking for a way to take a part of screen and put it in a multidimensional array.

So for instance, I want to load all pixel values between (100,100) and (200,200) to a array of 100 x 100 x 3 (3 for RGB)

I think I could make a screenshot, save it to an image. Load the image again, put the values in an array and take the desired part.

But that's a lot of overhead and I think there is a simpler way. Can someone point me to the right functions so I can build what I want?

Link to comment
Share on other sites

Maybe you could strip down, re-build, and modify this example to build what you want.

#include <Array.au3>

Local $x1 = 100, $y1 = 100, $x2 = 200, $y2 = 250

Local $aArr1 = _ArrayPixels($x1, $y1, $x2, $y2) ; Get Initial array

; Get initial checksum
Local $checksum = PixelChecksum($x1, $y1, $x2, $y2)

; ****** Wait for the region to change, the region is checked every 100ms to reduce CPU load ******
While $checksum = PixelChecksum($x1, $y1, $x2, $y2) ; Move a window in PixelChecksum location to trigger event and exit loop.
    Sleep(100)
WEnd

Local $aArr2 = _ArrayPixels($x1, $y1, $x2, $y2) ; Get changed array of pixel
Local $aArr3 = _ArraysCompare($aArr1, $aArr2)
_ArrayDisplay($aArr3)


; Creates an array of pixel colours in the hex. RGB colour format, 0xRRGGBB,
;  which represents degrees of the three(3) colour channels, Red, Green, and Blue.
; Where, 0xFFFFFF is white; 0xFF0000 is Red; 0x00FF00 is Green; 0x0000FF is Blue, and 0x000080 is a darken Blue.
; Pixels in rows $iX1 and $iX2, and pixels in columns $iY1 and $iY2 are included in the array.
Func _ArrayPixels($iX1, $iY1, $iX2, $iY2, $hWnd = "")
    Local $aRet[$iY2 - $iY1 + 1][$iX2 - $iX1 + 1]
    Local $iXCount = 0, $iYCount = 0
    For $j = $iY1 To $iY2
        For $i = $iX1 To $iX2
            $aRet[$iYCount][$iXCount] = "0x" & Hex(PixelGetColor($i, $j, $hWnd), 6)
            $iXCount += 1
        Next
        $iXCount = 0
        $iYCount += 1
    Next
    Sleep(10) ; To reduce CPU load
    Return $aRet
EndFunc   ;==>_ArrayPixels

; Returns an array, where:-
;  Blank means $aArray1[x][y] = $aArray2[x][y]; and
;  Contents of $aArray2[x][y] when $aArray1[x][y] <> $aArray2[x][y]
Func _ArraysCompare(ByRef $aArray1, ByRef $aArray2)
    Local $aRet[UBound($aArray1)][UBound($aArray1, 2)]
    For $j = 0 To UBound($aArray1, 2) - 1
        For $i = 0 To UBound($aArray1) - 1
            If $aArray1[$i][$j] = $aArray2[$i][$j] Then
                $aRet[$i][$j] = ""
            Else
                $aRet[$i][$j] = $aArray2[$i][$j]
            EndIf
        Next
    Next
    Sleep(10) ; To reduce CPU load
    Return $aRet
EndFunc   ;==>_ArraysCompare
Link to comment
Share on other sites

Thanks for the ideas.

PixelChecksum() and PixelGetColor() look like the functions I'm looking for.

 

would be easier to know the program you want to automate. is it a game ?

 

Yes. Game is getting boring and last 2 months I'm building scripts to automate the boring parts. Thinking bout the scripts and building them, is more fun than the game itself at the moment. In a week or 2 I have some free time and I start working with the idea's in this topic.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...