Hi there,
Since I'm not a programmer, I don't know if there is a logical explanation for this, but I was wondering why the ImageSearch function returns the upper left coords of the found image and not the center?
Why would someone click the uppermost corner of an image? Let's suppose you want to click on a button with curved edges like the buttons above the text field on the AHK forum when writing a post. When you select the button, ImageSearch will find it and moves the mouse to the upper left corner. However, you are not able to click the button because the button has curved edges and the pointer is therefore not on the button.
Of course, you can select e.g. the text inside a button as an image or implement OutputVarX+=10
OutputVarY+=10 in your code but why not just return the center coords of the found image?
I genuinely can't think of an example when returning the center of a found image won't be useful.
The function could look like this.
ImageSearch, FoundX0, FoundY0, FoundX1, FoundY1, X1, Y1, X2, Y2, ImageFile
Or return only the center of the image although I don't think this will be very handy.
ImageSearch, FoundCenterX, FoundCenterY, X1, Y1, X2, Y2, ImageFile
I wouldn't be surprised if people have thought of this and didn't implement it for obvious reasons. If so, please do so
Also, I know there are scripts made by community members that let you click the center of an image after it finds it but I'm just interested in why this isn't the standard.
PS. If you guys think this post belongs more on the 'Ask for Help' forum you can, of course, move it.
ImageSearch: Click center of found image
-
- Posts: 188
- Joined: 17 Jan 2019, 10:13
Re: ImageSearch: Click center of found image
Clicking on an image once it is found is only one use case of finding an image with ImageSearch. The most generic and useful information on the location of the image is its upper-left corner, especially since that is how the the coordinates of the screen and windows are laid out (with 0,0 being the upper-left most pixel). The center of an image is relatively arbitrary based on its size for most cases.
I can think of many more cases where finding the upper-left corner of the image is more useful than finding the middle of it. It corresponds to a known point on all images -- its corner. The center of an image won't even be a pair of integers for 75% of all images.
I can think of many more cases where finding the upper-left corner of the image is more useful than finding the middle of it. It corresponds to a known point on all images -- its corner. The center of an image won't even be a pair of integers for 75% of all images.
Re: ImageSearch: Click center of found image
In programming you usually take the least steps to get a specific result. Calculating an images center point is an additional step. Image search internally stores the upper right corners due to the way it searches.
With the information a user has they can easily get the centerpoint of the found image if necessary.
With the information a user has they can easily get the centerpoint of the found image if necessary.
Recommends AHK Studio
-
- Posts: 188
- Joined: 17 Jan 2019, 10:13
Re: ImageSearch: Click center of found image
Thanks for the reply's
@boiler, oke, but I assume an image size isn't rounded to 0 digits then? Or, why not just round off the found center coords? Has this to do with multiple unnecessary steps nnnik mentioned?
And why not just give the option as parameters that could be left out within the function?
ImageSearch, FoundX0, FoundY0, X1, Y1, X2, Y2, ImageFile [, FoundX1, FoundY1]-> not the greatest layout but just an example
Can you give an example of a case when finding the upper-left corner will be more useful then finding the center?
@nnnik,
@boiler, oke, but I assume an image size isn't rounded to 0 digits then? Or, why not just round off the found center coords? Has this to do with multiple unnecessary steps nnnik mentioned?
And why not just give the option as parameters that could be left out within the function?
ImageSearch, FoundX0, FoundY0, X1, Y1, X2, Y2, ImageFile [, FoundX1, FoundY1]-> not the greatest layout but just an example
Can you give an example of a case when finding the upper-left corner will be more useful then finding the center?
@nnnik,
Oke, this is a clear explanation. Btw, don't you mean upper-left corner?
Re: ImageSearch: Click center of found image
which pixel designates this 4x4 image's center? is it the red one? is it the green one? is it the blue one? or perhaps the yellow?
maybe we should add yet another flag to ImageSearch so u can specify which pixel u want the image's center to be?
or maybe we could have it return the top-left pixel every time, not bloat the function with more params, and let the programmer decide which pixel the center is supposed to located at
Last edited by swagfag on 22 May 2019, 10:49, edited 1 time in total.
Re: ImageSearch: Click center of found image
Swag stated it well. Also, the size of the image isn’t what is rounded because it’s always an integer. It’s only the center of the image that often wouldn’t be an integer because you’re dividing by 2, and if the size is an odd number, then the center will have a 0.5. Rounding the result would be a terrible thing to do. Then we don’t even have the ability to determine its exact location because we’ve thrown away the precision, so even if we try to determine it, we will often be off by one pixel.
The unnecessary steps are in finding the center in the first place because AHK finds the location relative to the corner when it searches. Not only is the center usually not needed, then you are often causing the programmer to do yet another unnecessary step to find the corner.
Here’s just one example of the corner being much more important than the center: I want to find the corner of a text box because I am going to perform a screen capture of it. I want to know the location of where the box starts, and the corner is what defines where to start grabbing the image. This isn’t a rare example. The corner is typically more useful than the center.
The unnecessary steps are in finding the center in the first place because AHK finds the location relative to the corner when it searches. Not only is the center usually not needed, then you are often causing the programmer to do yet another unnecessary step to find the corner.
Here’s just one example of the corner being much more important than the center: I want to find the corner of a text box because I am going to perform a screen capture of it. I want to know the location of where the box starts, and the corner is what defines where to start grabbing the image. This isn’t a rare example. The corner is typically more useful than the center.
-
- Posts: 188
- Joined: 17 Jan 2019, 10:13
Re: ImageSearch: Click center of found image
Edit the "+" for --> "+ or -" +x/y Coor
EJ:
foundY:=foundX+50
foundY:=foundY-80
if your img is "100x100"
use:
foundY:=foundX+50 ; 50% of your IMG
foundY:=foundY+50 ; 50% of your IMG
= Center of img.
EJ:
foundY:=foundX+50
foundY:=foundY-80
if your img is "100x100"
use:
foundY:=foundX+50 ; 50% of your IMG
foundY:=foundY+50 ; 50% of your IMG
= Center of img.
Code: Select all
ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, YourIMG.png
If(ErrorLevel == 0){
foundY:=foundX+300 ; <--- Edit for + or - Y Coor
foundX:=foundY+300 ; <--- Edit for + or - X Coor
Click, %foundX%, %foundY%
}
-
- Posts: 188
- Joined: 17 Jan 2019, 10:13
Re: ImageSearch: Click center of found image
@fkrause2, that would, indeed, be the way to go when you know the size of the image you are looking for (and the way I used).
However, my point was to click the center of an image you don't know the size of.
However, my point was to click the center of an image you don't know the size of.
Re: ImageSearch: Click center of found image
how would you not know the size of the image, if you are the one who is specifying the image file to search forAviationGuy wrote: ↑07 Feb 2020, 03:54However, my point was to click the center of an image you don't know the size of.
Re: ImageSearch: Click center of found image
I do agree that AutoHotkey should provide basic stuff like interaction with image files.
However it is also fairly simple to use GDIp to find the size of the image.
From then onwards its a rather simple math problem.
However it is also fairly simple to use GDIp to find the size of the image.
From then onwards its a rather simple math problem.
Recommends AHK Studio
-
- Posts: 188
- Joined: 17 Jan 2019, 10:13
Re: ImageSearch: Click center of found image
Of course, you can check the image size when you've created it yourself. But when you are making a script that clicks lots of buttons, this feels a bit repetitive to do and AHK is just there to eliminate repetitions.
Next to this, it doesn't feel like that big of a deal to add it to the function (not saying that it needs to be done, just discussing it here )
But I think the topic can be closed, thanks for the responses!
Next to this, it doesn't feel like that big of a deal to add it to the function (not saying that it needs to be done, just discussing it here )
As I mentioned before, when you use parameters that could be left out, anyone that doesn't want to use them can leave 'm out.AviationGuy wrote: ↑22 May 2019, 07:16And why not just give the option as parameters that could be left out within the function?
ImageSearch, FoundX0, FoundY0, X1, Y1, X2, Y2, ImageFile [, FoundX1, FoundY1]-> not the greatest layout but just an example
But I think the topic can be closed, thanks for the responses!
Re: ImageSearch: Click center of found image
You can probably make a function that does that for you.
Recommends AHK Studio
-
- Posts: 499
- Joined: 03 Dec 2018, 20:02
Re: ImageSearch: Click center of found image
@Masonjar13 wrote a function for getting the image size
Example function:
Example function:
Code: Select all
waitclick(img_path, btn := "", times := "", img_options := "") {
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
loop {
ImageSearch, found_x, found_y, 0, 0, % A_ScreenWidth, % A_ScreenHeight, % img_options ? img_options " " : "" . img_path
} until (!errorlevel)
splitPath,img_path,fN,fD
oS:=comObjCreate("Shell.Application")
oF:=oS.namespace(fD?fD:a_workingDir)
oFn:=oF.parseName(fD?fN:img_path)
size:=strSplit(oFn.extendedProperty("Dimensions"),"x"
," ?" . chr(8234) chr(8236))
x := found_x + (size[1]/2)
y := found_y + (size[2]/2)
click %x%, %y%, %btn%, %times%
}
waitclick("button.png")
-
- Posts: 3
- Joined: 10 Feb 2022, 04:34
Re: ImageSearch: Click center of found image
Wow, thanks so much for that, I was looking exactly at this problematic
Then made some minor changes, without touching that part, and now it clicks to a different location
I tried to remove the "foundY:=foundX+20", and it still clicks way off
Is that because it doesnt find the right image?
When I tried it first, the msgbox would say the image was found, so it seems like it recognizes an image, but it's not the same. Should I specify more things in the image search?
Anyone with an idea on how to resolve this would be amazing
That would really help me transfer that to different computers and settings, rather than just using xy click coordinates
Although, when I did it, it cliked like 400 pixels left that what I wanted.fkrause2 wrote: ↑17 Sep 2019, 14:53Code: Select all
ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, YourIMG.png If(ErrorLevel == 0){ foundY:=foundX+300 ; <--- Edit for + or - Y Coor foundX:=foundY+300 ; <--- Edit for + or - X Coor Click, %foundX%, %foundY% }
Then made some minor changes, without touching that part, and now it clicks to a different location
I tried to remove the "foundY:=foundX+20", and it still clicks way off
Is that because it doesnt find the right image?
When I tried it first, the msgbox would say the image was found, so it seems like it recognizes an image, but it's not the same. Should I specify more things in the image search?
Anyone with an idea on how to resolve this would be amazing
That would really help me transfer that to different computers and settings, rather than just using xy click coordinates
Code: Select all
^n::
Run, https://blabla
pageloaded := false
while(pageloaded = false)
{
sleep 1000
ImageSearch, vehiX, vehiY, 0, 0, A_ScreenWidth, A_ScreenHeight, vehicules.png
if (ErrorLevel == 0)
{
pageloaded := true
;~ vehiX:=vehiY+40
;~ vehiY:=vehiX+40
}
}
click, %vehiY%, %vehiX%
;MsgBox, Web paged loaded
Re: ImageSearch: Click center of found image
Math_the_rat wrote: ↑18 Feb 2022, 06:08Wow, thanks so much for that, I was looking exactly at this problematic
Although, when I did it, it cliked like 400 pixels left that what I wanted.fkrause2 wrote: ↑17 Sep 2019, 14:53Code: Select all
ImageSearch, foundX, foundY, 0, 0, %A_ScreenWidth%, %A_ScreenHeight%, YourIMG.png If(ErrorLevel == 0){ foundY:=foundX+300 ; <--- Edit for + or - Y Coor foundX:=foundY+300 ; <--- Edit for + or - X Coor Click, %foundX%, %foundY% }
Then made some minor changes, without touching that part, and now it clicks to a different location
I tried to remove the "foundY:=foundX+20", and it still clicks way off
Is that because it doesnt find the right image?
When I tried it first, the msgbox would say the image was found, so it seems like it recognizes an image, but it's not the same. Should I specify more things in the image search?
Anyone with an idea on how to resolve this would be amazing
That would really help me transfer that to different computers and settings, rather than just using xy click coordinates
Code: Select all
^n:: Run, https://blabla pageloaded := false while(pageloaded = false) { sleep 1000 ImageSearch, vehiX, vehiY, 0, 0, A_ScreenWidth, A_ScreenHeight, vehicules.png if (ErrorLevel == 0) { pageloaded := true ;~ vehiX:=vehiY+40 ;~ vehiY:=vehiX+40 } } click, %vehiY%, %vehiX% ;MsgBox, Web paged loaded
Code: Select all
Wrong Code:
foundY:=foundX+300 ; <--- Edit for + or - Y Coor
foundX:=foundY+300 ; <--- Edit for + or - X Coor
Working one:
foundY:=foundY+300 ; <--- Edit for + or - Y Coor
foundX:=foundX+300 ; <--- Edit for + or - X Coor
Who is online
Users browsing this forum: No registered users and 20 guests