How can I get the coordinates, width, and height of the area that I want to use for Imagesearch? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

How can I get the coordinates, width, and height of the area that I want to use for Imagesearch?

Post by LAPIII » 09 Jun 2022, 13:43

Also do you recommend searching within a bigger space, let's say 10% bigger?

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: How can I get the coordinates, width, and height of the area that I want to use for Imagesearch?

Post by boiler » 09 Jun 2022, 19:08

LAPIII wrote: How can I get the coordinates, width, and height of the area that I want to use for Imagesearch?
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.

Other than that, I’m not sure what kind of answer you would expect someone else to provide for this. You are the one who presumably knows where the image could potentially appear, and that defines the coordinates. I believe you already know how to use the Window Spy tool to get the locations of certain coordinates.
LAPIII wrote: Also do you recommend searching within a bigger space, let's say 10% bigger?
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.

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: How can I get the coordinates, width, and height of the area that I want to use for Imagesearch?

Post by LAPIII » 09 Jun 2022, 19:42

Did I make this correctly:

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

Can you tell me that difference between the error levels, 0 - 2?

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: How can I get the coordinates, width, and height of the area that I want to use for Imagesearch?

Post by boiler » 09 Jun 2022, 19:58

LAPIII wrote: Did I make this correctly:
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.

LAPIII wrote: Can you tell me that difference between the error levels, 0 - 2?
I don't know how to make it any more clear than what is already in the documentation:
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).
What is it about that description that you don't understand?

Rohwedder
Posts: 7509
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How can I get the coordinates, width, and height of the area that I want to use for Imagesearch?

Post by Rohwedder » 10 Jun 2022, 02:36

Hallo,
to get the screen coordinates of the area:

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

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: How can I get the coordinates, width, and height of the area that I want to use for Imagesearch?

Post by LAPIII » 10 Jun 2022, 09:12

@Rohwedder Thank you for the awesome script. I just don't know how to use any of those hotkeys, c, t , g, m. What is GUI number?

Rohwedder
Posts: 7509
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How can I get the coordinates, width, and height of the area that I want to use for Imagesearch?

Post by Rohwedder » 10 Jun 2022, 09:33

c, t, g, m are not Hotkeys but options of this slightly modified function of Learning one.
https://www.autohotkey.com/board/topic/57646-function-selectarea/
The effect of these options is described in the notes:

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

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: How can I get the coordinates, width, and height of the area that I want to use for Imagesearch?

Post by BoBo » 10 Jun 2022, 10:14

Adding whatever "kill switch"-option might make sense as opening the script's context menu via right-click for exiting the script seems not possible that way. :think: (task manager to the rescue).

Rohwedder
Posts: 7509
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How can I get the coordinates, width, and height of the area that I want to use for Imagesearch?

Post by Rohwedder » 10 Jun 2022, 11:08

Sorry, that was just an old (unfinished) snippet. Of course it is allowed to decorate RButton with modifiers.
E.G.:

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
And another FirstKey is also possible:

Code: Select all

#q:: ;FirstKey

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: How can I get the coordinates, width, and height of the area that I want to use for Imagesearch?

Post by LAPIII » 10 Jul 2022, 14:08

@Rohwedder Can you make your script copy the measurements to the clipboard, change the | to ,, and display in a MsgBox and not a ToolTip. Thanks bro.

Rohwedder
Posts: 7509
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How can I get the coordinates, width, and height of the area that I want to use for Imagesearch?  Topic is solved

Post by Rohwedder » 11 Jul 2022, 01:11

This current version could:

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
}

LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: How can I get the coordinates, width, and height of the area that I want to use for Imagesearch?

Post by LAPIII » 06 Feb 2023, 11:27

I don't know how to use the options, specifically ; - m CoordMode. Default: s. s = Screen, r = Relative

User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: How can I get the coordinates, width, and height of the area that I want to use for Imagesearch?

Post by boiler » 06 Feb 2023, 11:58

LAPIII wrote: I don't know how to use the options, specifically ; - m CoordMode. Default: s. s = Screen, r = Relative
The example on the next line shows you:

Code: Select all

SelectArea("cLime t100 g55 mr")
In case it's still not clear, it's mr in that example. For screen, it would be ms.

Post Reply

Return to “Ask for Help (v1)”