This is an old revision of the document!
Table of Contents
Image Search with AutoIt
Going to move this to GitHub soon, now that I know how to Git
Overview
I came across the need to be able to find something on the screen based on a reference image in my AutoIt scripts. However, this is not an available feature of AutoIt. There are functions available for checking individual pixels, but not images.
I found a solution in this thread. The key is the AutoHotkey ImageSearch function. Someone extracted the ImageSearch code from AutoHotkey, packed it up in to convenient DLLs, and put together a UDF 1) library that acts as a wrapper.
I took the UDF library and made the following improvements to it:
- Updated relevant functions to return an array of [x,y] coordinates on success, and False on failure. Original search functions returned True/False success status, while coordinate results were stored in external variables passed in by reference.
- Updated relevant functions with an extra parameter for a window handle, making it possible to limit the search area to the boundaries of a specified window.
- Added the ability to handle multi-monitor desktops by re-defining desktop boundaries in to new Global variables $desktopLeft, $desktopTop, $desktopRight, $desktopBottom, $desktopWidth, and $desktopHeight.
Syntax
The best intro to the syntax is in my example code…
; includes #include <ImageSearch.au3> ; for _ImageSearch, _ImageSearchArea, _WaitForImageSearch, _WaitForImagesSearch ; Ref: https://www.autoitscript.com/autoit3/docs/functions/AutoItSetOption.htm ; Enforce pre-declaration of variables, helps develop good coding habits AutoItSetOption("MustDeclareVars", 1) ; Set the mouse coordinate mode to screen absolute ; 0 = relative to active window ; 1 = absolute screen ; 2 = relative to client area of active window AutoItSetOption("MouseCoordMode", 1) ; This will be the target image file to search for Local $target = ".\ImageSearch_target.png" ; Declare an init a variable to hold the search result Local $result = 0 ; EXAMPLE 1 ; Find the image in a specified area, and move the mouse to its location $result = _ImageSearchArea($target, 1, $desktopLeft, $desktopTop, $desktopWidth, $desktopHeight, 0) If IsArray($result) Then MouseMove($result[0], $result[1]) ; EXAMPLE 2 ; Find the image across the entire desktop $result = _ImageSearch($target, 1) If IsArray($result) Then MouseMove($result[0], $result[1]) ; The above examples accomplish the same goal, in fact _ImageSearch actually ; calls _ImageSearchArea with predetermined parameters
More in-depth function documentation is included in the comments of ImageSearch.au3.
Download
ImageSearch is my updated package. It includes the original ImageSearch and MSVC DLLs, along with the updated ImageSearch.au3 library.