Jump to content

ofLight's Pixelchecksum script


Recommended Posts

Ok, I have been looking at ofLight's PixelCheckSum script, but how can i make the script look for multiple images / PixelCheckSum locations?

My case is that I have 6 pictures, and I want the script to recognize 3 of the pictures, that I have already defined those 3 in the .INI file in advance. So if the script succeeds in finding the images, then it would move the mouse over them and click on them for instance. :)

So:

1. Recognize the images that I have to defined as "PixelColor" and "PixelCheckSum in the .INI file".

2. If it finds the images, the return positive value, or call a function.

3. If it don't find the pictures, then return a negative value or call another function.

I Know this is very easy to do when I've already got the main script, I just need some help adjusting it. My main problem is how to handle the .INI to support multiple "PixelCheckSum's" :(

By the way; the original script is made for "recording" locations on the screen, and then move the mouse over the location if you want to find them. :D

Here is the script I use as starting point that i need adjusted (all respect to ofLight for making this!) :

HotKeySet("f","_find")
HotKeySet("r","record")
HotKeySet('{esc}', '_Exit')

Global $msg =   "Press R to Record " & @LF & _
                "Press F to Find " & @LF & _
                "info saved in Pixel.ini"
Global $xy,$currentpixel

;If $pixel = -1 Or $chksum = -1 Then
;   MsgBox(4096,"Error","Could not read INI")
;   Exit
;EndIf

While 1
    $xy = MouseGetPos()
    $currentpixel = PixelGetColor($xy[0],$xy[1])
    ToolTip("Pixel color = " & $currentpixel & @LF & $msg)
    Sleep(100)
WEnd

Func record()
    IniWrite(".\Pixel.ini","Main","PixelColor",$currentpixel)
    Local $chksum = PixelChecksum($xy[0]-5, $xy[1]-5, $xy[0]+5, $xy[1]+5)
    IniWrite(".\Pixel.ini","Main","PixelCheckSum",$chksum)
    MsgBox(4096,"","Pixel Area Recorded." & @LF & @LF & "See INI file for details.", 2)
  ;Exit
EndFunc

Func _find()
    Local $pixel =  Int(IniRead(".\Pixel.ini","Main","PixelColor","-1"))
    Local $chksum = Int(IniRead(".\Pixel.ini","Main","PixelCheckSum","-1"))
   
    $sv = 0
    $w = 1
    $x = 5;LEFT pixel of Total Area to Search for $pixel
    $y = 5;TOP
    $xpixel = @DesktopWidth - 5;Right
    $ypixel = @DesktopHeight - 5;Bottom
   
    While $w = 1
        $xy = PixelSearch($x,$y,$xpixel,$ypixel,$pixel, $sv)
        If $w = 3 Then
            Exit
        EndIf

        If @error And $ypixel = (@DesktopHeight - 5)Then
            MsgBox(4096,"@Error ","Could not find Checksum" & @LF & '    Finished Searching all of Screen', 4)
            SetError(1)
            Return 0
        ElseIf @error Then
            $y = $ypixel + 1
            $ypixel = (@DesktopHeight - 5)
            $x = 0
          ;_cont()
          ;MsgBox(4096,"@Error","Could not find Checksum" & @LF & '  looking again in 2sec', 2)
        ElseIf $chksum = PixelCheckSum($xy[0]-5, $xy[1]-5, $xy[0]+5, $xy[1]+5) Then
            $w = 3  ;PixelCheckSum(left, top, right, bottom [, step] )
            MouseMove($xy[0],$xy[1], 30)
            ToolTip("Found Checksum")
                $pos = MouseGetPos()
                MouseMove( $pos[0], $pos[1] -5, 20);Top
                MouseMove( $pos[0] +5, $pos[1], 20);Right
                MouseMove( $pos[0], $pos[1] +5, 20);Bottom
                MouseMove( $pos[0] -5, $pos[1], 20);Left
          ;Exit
        Else
            $y = $xy[1]
            $ypixel = $y
            $x = $xy[0] + 1
        EndIf
    WEnd
EndFunc

Func _cont()
    MsgBox(4096,"@Error","Could not find Checksum" & @LF & '     looking again in 2sec', 2)
    _find() 
EndFunc 

Func _Exit()
    ToolTip('         '&@CRLF&'  EXITING  '&@CRLF&'       ')
    Sleep(500)
    Exit
EndFunc
Edited by Zipper
Link to comment
Share on other sites

Try this:

Global $iCount=0
...
Func record()
    IniWrite(".\Pixel.ini",StringFormat("Selection%05d",$iCount),"PixelColor",$currentpixel)
    Local $chksum = PixelChecksum($xy[0]-5, $xy[1]-5, $xy[0]+5, $xy[1]+5)
    IniWrite(".\Pixel.ini",StringFormat("Selection%05d",$iCount),"PixelCheckSum",$chksum)
    $iCount += 1
    MsgBox(4096,"","Pixel Area Recorded." & @LF & @LF & "See INI file for details.", 2)
  ;Exit
EndFunc
Link to comment
Share on other sites

Ok, now that I have the script writing the .INI file correctly. I need some assistance on how to read every selection inside the .INI without repeating this code:

$var = IniReadSection(@ScriptDir & "\Pixel.ini", "Selection00001")
If @error Then 
    MsgBox(4096, "", "Error occurred, probably no INI file.")
Else
    For $i = 1 To $var[0][0]
        MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF & "Value: " & $var[$i][1])
    Next
EndIf

I want this to be almost like the code Zorphnog posted, just that it reads the .INI selections one by one, and then look if one or more of the PixelCheckSums match the pictures.

If I get that working I can have the script up and running soon. :)

Edited by Zipper
Link to comment
Share on other sites

Local $aResult, $i=0, $sMsg
If FileExists(@ScriptDir & "\Pixel.ini") Then
    $sMsg = ""
    $aResult = IniReadSection(@ScriptDir & "\Pixel.ini", StringFormat("Selection%05d", $i))
    While Not @error
        If $aResult[0][0] = 2 Then 
            $sMsg &= StringFormat("Selection%05d   %s: %08d   %s: %010d\r\n", $i, $aResult[1][0], $aResult[1][1], $aResult[2][0], $aResult[2][1])
        EndIf
        $i += 1
        $aResult = IniReadSection(@ScriptDir & "\Pixel.ini", StringFormat("Selection%05d", $i))
    WEnd
     MsgBox(4096, "", $sMsg)
Else
    MsgBox(4096, "", "Error occurred, no INI file.")
EndIf

Link to comment
Share on other sites

  • 2 weeks later...

Ah, thanks Zorphnog! :)

I've got a better understanding of how we can handle .INI files, but I have been fiddling around with how we can get the script searching for all the pictures that I have "saved" in the .INI file.

I need the script to search for one value, if it don't find the color, then proceed to the next value in the .INI and so on. And if it find a match with the PixelCheckSum values, then mouseclick on the picture and continue search. :)

When I try to fetch the values in the .INI for the searching function, I just get the whole caboodle if you know what I mean.

This code fetches the data out to a messagebox, the problem is to hande each "[selection]" differently in the searching for pictures function :)

Local $aResult, $i=0, $sMsg
If FileExists(@ScriptDir & "\Pixel.ini") Then
    $sMsg = ""
    $aResult = IniReadSection(@ScriptDir & "\Pixel.ini", StringFormat("Selection%05d", $i))
    While Not @error
        If $aResult[0][0] = 2 Then
            $sMsg &= StringFormat("Selection%05d   %s: %08d   %s: %010d\r\n", $i, $aResult[1][0], $aResult[1][1], $aResult[2][0], $aResult[2][1])
        EndIf
        $i += 1
        $aResult = IniReadSection(@ScriptDir & "\Pixel.ini", StringFormat("Selection%05d", $i))
    WEnd
     MsgBox(4096, "", $sMsg)
Else
    MsgBox(4096, "", "Error occurred, no INI file.")
EndIf

I hope someone understands what I mean in spite of my bad explanation :/

Edited by Zipper
Link to comment
Share on other sites

I didnt test this much so if anyone finds any buggs please let me know. Since you will be searching for multiple checksums I added the Search Area for each checksum into the Ini, if you define each of these seperate(instead of just searching the whole screen) it should greatly increase the overall speed.

EDIT: If you wanted, useing this same format you could also add in stuff like the size of each checksum and what type of action to take for each entry(Left click, Right click, Double click, etc.).

HotKeySet("f","_find")
HotKeySet("r","_record")
HotKeySet('{esc}', '_Exit')

Global $msg = "Press R to Record "&@LF&"Press F to Find "&@LF&"info saved in Pixel.ini"
Global $IniFile = ".\Pixel.ini"
Global $xy,$currentpixel,$iCount=0

_InitializeINI()

While 1
    $xy = MouseGetPos()
    $currentpixel = PixelGetColor($xy[0],$xy[1])
    ToolTip("Pixel color = " & $currentpixel & @LF & $msg)
    Sleep(10)
WEnd

Func _record()
    $currentTotal = IniRead($IniFile, "Settings", "Total", "Error")
    $currentTotal = $currentTotal+1
    IniWrite($IniFile, "Settings", "Total", $currentTotal)
    IniWrite($IniFile, $currentTotal, "PixelColor", $currentpixel)
    $chksum = PixelChecksum($xy[0]-5, $xy[1]-5, $xy[0]+5, $xy[1]+5)
    IniWrite($IniFile, $currentTotal, "PixelCheckSum", $chksum)
    IniWrite($IniFile, $currentTotal, "Left_SearchArea", "0")
    IniWrite($IniFile, $currentTotal, "Top_SearchArea", "0")
    IniWrite($IniFile, $currentTotal, "Right_SearchArea", @DesktopWidth)
    IniWrite($IniFile, $currentTotal, "Bottom_SearchArea", @DesktopHeight&@CRLF)    
    ToolTip('        '&@CRLF&'  SAVED '&@CRLF&'          ')
    Sleep(1500)
EndFunc

Func _find()
    
    Dim $Settings[9]
    $Settings[0] = 8
    $Settings[1] = IniRead($IniFile, "Settings", "Total", "Error")
    If $Settings[1] > 0 Then
        For $i = 1 to $Settings[1]  
            $Settings[2] = IniRead($IniFile, $i, "PixelColor", "Error")
            $Settings[3] = IniRead($IniFile, $i, "PixelCheckSum", "Error")
            $Settings[4] = IniRead($IniFile, $i, "Left_SearchArea", "Error")
            $Settings[5] = IniRead($IniFile, $i, "Top_SearchArea", "Error")
            $Settings[6] = IniRead($IniFile, $i, "Right_SearchArea", "Error")
            $Settings[7] = IniRead($IniFile, $i, "Bottom_SearchArea", "Error")
            $Settings[8] = IniRead($IniFile, $i, "ShadeVar", "0")
            $w = 1
            $AbsoluteLeft = $Settings[4]
            $AbsoluteBottom = $Settings[7]
           
            While $w = 1
                $xy = PixelSearch($Settings[4],$Settings[5],$Settings[6],$Settings[7],$Settings[2],$Settings[8])
                If @error And $Settings[7] = $AbsoluteBottom Then
                    ToolTip('        '&@CRLF&'  Could not find Checksum '&$i&@CRLF&'          ')
                    Sleep(1500)
                    $w = 2
                ElseIf @error Then
                    $Settings[5] = $Settings[7] + 1
                    $Settings[7] = $AbsoluteBottom
                    $Settings[4] = $AbsoluteLeft
                ElseIf $Settings[3] = PixelCheckSum($xy[0]-5, $xy[1]-5, $xy[0]+5, $xy[1]+5) Then
                    $w = 3  
                    MouseMove($xy[0],$xy[1], 30)
                    ToolTip("Found Checksum")
                    $pos = MouseGetPos()
                    MouseMove( $pos[0], $pos[1], 20);Top

                Else
                    $Settings[5] = $xy[1]
                    $Settings[7] = $Settings[5]
                    $Settings[4] = $xy[0] + 1
                EndIf
            WEnd
        Next
    Else
        MsgBox(0,"ERROR","No Saved Checksums to search for yet")
    EndIf
    
EndFunc

Func _Exit()
    ToolTip('        '&@CRLF&'  EXITING  '&@CRLF&'          ')
    Sleep(500)
    Exit
EndFunc

Func _InitializeINI() ;Create INI if does not exsist
    $val1 = IniRead($IniFile, "Settings", "Total", -1)
    If $val1 = -1 then 
        IniWrite($IniFile, "Settings", "Total", "0"&@CRLF)
    EndIf
EndFunc
Edited by ofLight

There is always a butthead in the crowd, no matter how hard one tries to keep them out.......Volly

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...