[Function] ImageSearchAll - Find all instances of an image

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
boiler
Posts: 16771
Joined: 21 Dec 2014, 02:44

[Function] ImageSearchAll - Find all instances of an image

Post by boiler » 24 Feb 2022, 23:29

This is a simple but useful function. It occasionally gets asked in "Ask For Help" how all instances of an image can be found, and using this function can be quicker/easier than a GDIP-based approach. I'm posting it here so I can easily point to it when asked. Thanks to @Xtra for fixing the issue it had initially.

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
Return value:
  • 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
Usage example:

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 ")"

maxkill
Posts: 158
Joined: 11 Apr 2016, 13:03

Re: [Function] ImageSearchAll - Find all instances of an image

Post by maxkill » 27 Jan 2023, 15:46

boiler wrote:
24 Feb 2022, 23:29
code
how do we make it work
i saved an png file of a image and saved the script
upon clicking on it nothing happens

need more instructions

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 " " needle1.png
			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
}

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

Re: [Function] ImageSearchAll - Find all instances of an image

Post by boiler » 27 Jan 2023, 15:52

You are not using the function correctly -- that's not how you use functions in general. You don't change the function. You include the function exactly as is, then you call the function, passing your specific parameters to it. See the usage example from my post.

Study the documentation on what functions are and how to use them. You should also study expressions because even if putting your image file name into the function was the right thing to do (which it is not), you did not use the correct syntax to do so (unquoted literal string). If you have further questions about how to call functions and AHK syntax, post in the "Ask for Help" forum as giving direction on AHK fundamentals is outside the scope of this thread.

maxkill
Posts: 158
Joined: 11 Apr 2016, 13:03

Re: [Function] ImageSearchAll - Find all instances of an image

Post by maxkill » 27 Jan 2023, 18:25

boiler wrote:
27 Jan 2023, 15:52
You are not using the function correctly -- that's not how you use functions in general. You don't change the function. You include the function exactly as is, then you call the function, passing your specific parameters to it. See the usage example from my post.

Study the documentation on what functions are and how to use them. You should also study expressions because even if putting your image file name into the function was the right thing to do (which it is not), you did not use the correct syntax to do so (unquoted literal string). If you have further questions about how to call functions and AHK syntax, post in the "Ask for Help" forum as giving direction on AHK fundamentals is outside the scope of this thread.
Still no idea what to do so I guess I will have to do that then.

Post Reply

Return to “Scripts and Functions (v1)”