The function:
Code: Select all
ImageSearchAll(imageFile, x1:=0, y1:=0, x2:="Screen", y2:="Screen", var:=0) {
x2 := x2 = "Screen" ? A_ScreenWidth : x2
y2 := y2 = "Screen" ? A_ScreenHeight : y2
found := []
y := y1
loop {
x := x1
lastFoundY := 0
loop {
ImageSearch, foundX, foundY, x, y, x2, y2, % "*" var " " imageFile
if (ErrorLevel = 2)
return -1
if !ErrorLevel {
if (lastFoundY = 0 || lastFoundY = foundY) {
found.Push({x: foundX, y: foundY})
x := foundX + 1
lastFoundY := foundY
} else
break
}
} until ErrorLevel
y := lastFoundY + 1
} until (x = x1) && ErrorLevel
return found
}
Parameters:
- imageFile: Relative or full path to reference image file
- x1, y1, x2, y2: Search rectangle. Leave all blank to search 0, 0, A_ScreenWidth, A_ScreenHeight
- var: Allowable variation in shade
- Found coordinates are returned as a simple array of coordinate pairs
- Each coordinate pair is an associative array with keys "x" and "y"
- .Count() of returned array indicates number of found images (0 for none)
- -1 is returned if there was a problem such as failure to open image
Code: Select all
#Include ImageSearchAll.ahk
CoordMode, Pixel, Screen
Locations := ImageSearchAll("MyImage.png", 100, 150, 500, 300)
for Index, Location in Locations
MsgBox, % "Location " Index " of " Locations.Count() ": (" Location.x "," Location.y ")"