[Function] ImageSearchAll - Find all instances of an image

Post your working scripts, libraries and tools.
User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

[Function] ImageSearchAll - Find all instances of an image

Post by boiler » 10 Jun 2023, 14:47

Here is a v2 translation of ImageSearchAll for v1, a lightweight function for finding all instances of an image:

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
		while f := ImageSearch( &foundX, &foundY, x, y, x2, y2, "*" var " " imageFile) {
			if (lastFoundY = 0 || lastFoundY = foundY) {
				found.Push(Map("x", foundx, "y", foundy))
				x := foundX + 1
				lastFoundY := foundY
			} else
				break
		}
		y := lastFoundY + 1
	} until (x = x1) && !f
	return found
}

Example usage:

Code: Select all

#Requires AutoHotkey v2.0
#Include ImageSearchAll.ahk

Found := ImageSearchAll("MyImage.png", 0, 0, 1000, 900)
Output := ""
for Instance, Coord in Found
	Output .= "Instance " Instance " found at: " Coord["x"] "," Coord["y"] "`n"
MsgBox Output

Return to “Scripts and Functions (v2)”