Both sides previous revisionPrevious revisionNext revision | Previous revision |
imagesearch [2018/04/04 11:50] – thekojukinator | imagesearch [2018/05/10 15:09] (current) – thekojukinator |
---|
====== Image Search with AutoIt ====== | ====== ImageSearch with AutoIt ====== |
| |
===== Overview ===== | ===== Overview ===== |
| |
For years, I've been using [[https://www.autoitscript.com|AutoIt]] to accomplish all sorts of automation, and it has proven to be a very capable tool. However, one feature it is missing, which I recently found necessary, is //image search//. Essentially, I wanted to be able to look for something on the screen that matched an image, and, if possible, with tolerance for imperfect matches. | I needed the ability for [[https://www.autoitscript.com|AutoIt]] to find something on the screen matching a reference image. Unfortunately, this is not an available built-in feature. There are functions available for checking individual pixels, but no way to match groups of pixels or images. |
| |
After some time searching around, I ran in to [[https://www.autoitscript.com/forum/topic/148005-imagesearch-usage-explanation/?page=4|this thread]], which pretty much took me 95% of the way to my desired solution. The key is [[https://www.autohotkey.com/docs/commands/ImageSearch.htm|AutoHotkey]]'s //ImageSearch// function. Someone capable extracted the //ImageSearch// function from the //AutoHotkey// and packed it up in to convenient DLLs, this started the ball rolling. With the contributions of some other users, a //UDF// ((User Defined Function library, in AutoIt.)) with wrapper functions was put together, and then packaged with all the necessary DLLs for convenience. | I found a solution in [[https://www.autoitscript.com/forum/topic/148005-imagesearch-usage-explanation/?page=4|this thread]]. The key is the [[https://www.autohotkey.com/docs/commands/ImageSearch.htm|AutoHotkey ImageSearch]] function. Someone extracted the **ImageSearch** code from //AutoHotkey//, packed it up in to convenient DLLs, and put together a **UDF** library that acts as a wrapper. |
| |
Missing from the wrapper functions was the ability to search within a specific window. Instead, the choices were to search the entire desktop or a specified area. I modified the code and added an optional parameter for a window handle, which will supply coordinates and dimensions for the search area. | I took the **UDF** library and made the following improvements to it: |
| |
===== Syntax ===== | * 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''. |
| |
Coming soon™... | ===== Example ===== |
| |
| <code autoit> |
| ; Include the ImageSearch library |
| #include ".\ImageSearch\ImageSearch.au3" |
| ; This will be the target image file to search for |
| Local $target = "DemoTarget.png" |
| ; Declare an init a variable to hold the search result |
| Local $result = False |
| ; Find the image across the entire desktop |
| $result = _ImageSearch($target, 1) |
| ; If found, move the mouse to it |
| If IsArray($result) Then MouseMove($result[0], $result[1]) |
| </code> |
| |
===== Download ===== | ===== Download ===== |
| |
{{:imagesearch.7z|ImageSearch}} is the package with the original DLLs, and my modified //ImageSearch.au3// include library. | I've put it up on [[https://github.com/TheKojukinator/ImageSearch|GitHub]]. |
| |
| {{tag>computing scripting autoit}} |