How can I get the coordinates, width, and height of the area that I want to use for Imagesearch?
Posted: 09 Jun 2022, 13:43
Also do you recommend searching within a bigger space, let's say 10% bigger?
Let's help each other out
https://www.autohotkey.com/boards/
https://www.autohotkey.com/boards/viewtopic.php?f=76&t=105151
Well, one important thing to realize is that ImageSearch’s parameters do not include width and height. The parameters include the x,y coordinates of the upper-left corner of the search rectangle and the x,y coordinates of the lower-right corner.
No, there is no reason to do that. Just make sure the image you are searching for would fall completely within the defined search rectangle.
Code: Select all
^+u::
Run, C:\Program Files\Waterfox\waterfox.exe postimages.org
WinWaitActive, Postimages ahk_exe waterfox.exe
ImageSearch, OutputVarx, OutputVarY, 759, 618, 1155, 674, Choose ImageSearch.PNG
if (ErrorLevel = 0)
Click 929 644
return
It generally looks correct, but I don't know if your coordinates are correct, you're basing them off the right reference point (i.e., window vs. screen coordinates per CoordMode), the image file is in the correct place, the image file is a good representation of the image to be found, etc.
I don't know how to make it any more clear than what is already in the documentation:
What is it about that description that you don't understand?ErrorLevel is set to 0 if the image was found in the specified region, 1 if it was not found, or 2 if there was a problem that prevented the command from conducting the search (such as failure to open the image file or a badly formatted option).
Code: Select all
; First press only RButton and define the area.
; Whenever the LButton is pressed additionally, the area can be moved.
; When RButton is released, the coordinates are displayed.
Rbutton:: ;FirstKey
ToolTip,% SelectArea("ms",SecondKey:="LButton") ;ms = CoordMode Screen
Return
SelectArea(Options:="",SecondKey:="")
{ ; from Learning one, slightly modified
/*
Returns selected area. Return example: 22|13|243|543
Options: (White space separated)
- c color. Default: Blue.
- t transparency. Default: 50.
- g GUI number. Default: 99.
- m CoordMode. Default: s. s = Screen, r = Relative
Examples: SelectArea() , SelectArea("cLime t100 g55 mr")
*/
CoordMode, Mouse, Relative
MouseGetPos, rMX, rMY
CoordMode, Mouse, Screen
MouseGetPos, MX, MY
loop, parse, Options, %A_Space%
{
Field := A_LoopField
FirstChar := SubStr(Field,1,1)
if FirstChar contains c,t,g,m
{
StringTrimLeft, Field, Field, 1
%FirstChar% := Field
}
}
c := (c = "") ? "Blue" : c, t := (t = "") ? "50" : t, g := (g = "") ? "99" : g , m := (m = "") ? s : m
Gui %g%: Destroy
Gui %g%: +AlwaysOnTop -caption +Border +ToolWindow +LastFound
WinSet, Transparent, %t%
Gui %g%: Color, %c%
;Hotkey := RegExReplace(A_ThisHotkey,"^(\w* & |\W*)")
RegExMatch(A_ThisHotkey, "\W$|\w*$", Hotkey)
While, (GetKeyState(Hotkey, "p"))
{
Sleep, 10
MouseGetPos, MXend, MYend
IF GetKeyState(SecondKey,"P")
MX += MXend - MXOld, MY += MYend - MYOld
w := Abs(MX - MXend), h := Abs(MY - MYend)
X := Min(MX, MXOld:=MXend), Y := Min(MY, MYOld:=MYend)
Gui %g%: Show, x%X% y%Y% w%w% h%h% NA
}
Gui %g%: Destroy
if m = s ; Screen
{
MouseGetPos, MXend, MYend
If (MX > MXend)
temp := MX, MX := MXend, MXend := temp
If (MY > MYend)
temp := MY, MY := MYend, MYend := temp
Return MX "|" MY "|" MXend "|" MYend
}
else ; Relative
{
CoordMode, Mouse, Relative
MouseGetPos, rMXend, rMYend
If (rMX > rMXend)
temp := rMX, rMX := rMXend, rMXend := temp
If (rMY > rMYend)
temp := rMY, rMY := rMYend, rMYend := temp
Return rMX "|" rMY "|" rMXend "|" rMYend
}
}
Code: Select all
; First press Win + RButton, leave RButton pressed and define the range.
; Whenever the LButton is pressed additionally, the area can be moved.
; When RButton is released, the coordinates are displayed.
#Rbutton:: ;FirstKey
ToolTip,% SelectArea("ms",SecondKey:="LButton") ;ms = CoordMode Screen
Return
Code: Select all
#q:: ;FirstKey
Code: Select all
; First press Win + RButton, leave RButton pressed and define the range
; Whenever the LButton is pressed additionally, the area can be moved.
; When RButton is released, the diagonal coordinates are displayed.
#RButton::MsgBox,% ClipBoard := SelectArea("ms", "LButton", ",")
; RButton = FirstKey
; ms = CoordMode Screen
; LButton = SecondKey
SelectArea(Options:="", SecondKey:="", Coord_Separator:="|")
{ ; from Learning one, modified by Rohwedder 11.07.2022
; https://www.autohotkey.com/board/topic/57646-function-selectarea/
; Returns the diagonal coordinates of the selected area
; Example with default Coord_Separator: 22|13|243|543
; Options: (White space separated)
; - c color. Default: Blue.
; - t transparency. Default: 50.
; - g GUI number. Default: 99.
; - m CoordMode. Default: s. s = Screen, r = Relative
; Examples: SelectArea() , SelectArea("cLime t100 g55 mr")
Loop, Parse, Options, %A_Space%
If InStr("ctgm", FirstChar := SubStr(A_LoopField, 1, 1))
%FirstChar% := SubStr(A_LoopField, 2)
c:=(c="")?"Blue":c, t:=(t="")?"50":t, g:=(g ="")?"99":g
CoordMode, Mouse, Relative
MouseGetPos, XR, YR
CoordMode, Mouse, Screen
MouseGetPos, MX, MY
XR -= MX, XR *= m := m = "r", YR -= MY, YR *= m
Gui %g%: Destroy
Gui %g%: +AlwaysOnTop -caption +Border +ToolWindow +LastFound
WinSet, Transparent, %t%
Gui %g%: Color, %c%
RegExMatch(A_ThisHotkey, "\W$|\w*$", Hotkey)
While, GetKeyState(Hotkey, "P")
{
Sleep, 10
MouseGetPos, MXend, MYend
IF GetKeyState(SecondKey, "P")
MX += MXend - MXold, MY += MYend - MYold
W := Abs(MX - MXend), H := Abs(MY - MYend)
X := Min(MX, MXold:=MXend), Y := Min(MY, MYold:=MYend)
Gui %g%: Show, x%X% y%Y% w%W% h%H% NA
}
Gui %g%: Destroy
MouseGetPos, MXend, MYend
Return, Min(MX,MXend)+XR Coord_Separator Min(MY,MYend)+YR
. Coord_Separator Max(MX,MXend)+XR Coord_Separator Max(MY,MYend)+YR
}
The example on the next line shows you:
Code: Select all
SelectArea("cLime t100 g55 mr")