Long-form README: https://chunjee.github.io/graphicsearch.ahk
A lightning-fast, highly versatile, and powerful screen searching library for AutoHotkey
GraphicSearch offers a powerful alternative to the native AHK Imagesearch command. While the native command requires saved image files, nearly exact image matching, and can be slow and difficult to troubleshoot, GraphicSearch takes a different approach. It abstracts the screen into a simplified representation, similar to ASCII art, using characters like 0s and _s. This abstraction enables faster matching, easier adjustments to fault tolerance, and more flexibility. Unlike AHK ImageSearch, which returns only the first match, GraphicSearch can return all matches, making it more efficient for complex searches. Additionally, it avoids the need to recapture the screen image for each search, improving performance when checking multiple graphics.
In a terminal or command line:
npm install graphicsearch.ahk
In your code only export.ahk needs to be included:
#Include %A_ScriptDir%\node_modules
#Include graphicsearch.ahk\export.ahk
oGraphicSearch := new graphicsearch()
result := oGraphicSearch.search("|<HumanReadableTag>*165$22.03z")
; => [{1: 1215, 2: 407, 3: 22, 4: 10, id: "HumanReadableTag", x: 1226, y: 412}]
You may also review or copy the library from ./export.ahk on GitHub #Incude as you would normally when manually downloading.
finds GraphicSearch queries on the screen
The GraphicSearch query(s) to search. Must be concatinated with
|
if searching multiple graphics
The options object
the search scope's upper left corner coordinates
the search scope's lower right corner coordinates
Fault tolerance of graphic and background (0.1=10%)
Wether or not to capture a new screenshot or not. If the value is 0, the last captured screenhhot will be used
Wether or not to find all instances or just one.
Join all GraphicsSearch queries for combination lookup.
Set the Max offset for combination lookup
(Array) Return an array of objects containing all lookup results, else false
if no matches were found.
optionsObj := { x1: 0
, y1: 0
, x2: A_ScreenWidth
, y2: A_ScreenHeight
, err1: 0
, err0: 0
, screenshot: 1
, findall: 1
, joinqueries: 1
, offsetx: 1
, offsety: 1 }
oGraphicSearch.search("|<tag>*165$22.03z", optionsObj)
oGraphicSearch.search("|<tag>*165$22.03z", {x2: 100, y2: 100})
performs the last .search with the last arguments supplied
oGraphicSearch.search("|<tag>*165$22.03z", {x2: 1028, y2: 720})
oGraphicSearch.searchAgain()
oGraphicSearch.searchAgain("|<HumanReadableTag>*99$26.z7z")
.scan(graphicsearch_query [, y1, x2, y2, err1, err0, screenshot, findall, joinqueries, offsetx, offsety])
finds GraphicSearch queries on the screen
GraphicsSearch queries as strings. Can be multiple queries separated by
|
the search scope's upper left corner coordinates
the search scope's lower right corner coordinates
A number between 0 and 1 (0.1=10%) for fault tolerance of foreground (err1) and background (err0)
if the value is 1, a new capture of the screen will be used; else it will use the last capture
if the value is 1, graphicsearch will find all matches. for 0, only return one match
if the value is 1, Join all GraphicsSearch queries for combination lookup
Set the Max offset for combination lookup
(Array) Return an array of objects containing all lookup results, else false
if no matches were found.
Any result is an associative array {1:X, 2:Y, 3:W, 4:H, x:X+W//2, y:Y+H//2, id:tag}. All coordinates are relative to Screen, colors are in RGB format, and combination lookup must use uniform color mode
oGraphicSearch.scan("|<tag>*165$22.03z", 1, 1, 1028, 720, .1, .1, 1, 1, 1, 0, 0)
; => [{1: 1215, 2: 400, 3:22, 4: 10, id: "tag", x:1226, y:412}]
.scanAgain([graphicsearch_query, y1, x2, y2, err1, err0, screenshot, findall, joinqueries, offsetx, offsety])
performs the last .search with the last arguments supplied
oGraphicSearch.scan("|<tag>*165$22.03z", {x2: 1028, y2: 720})
oGraphicSearch.scanAgain()
oGraphicSearch.scanAgain("|<HumanReadableTag>*99$26.z7z")
.find(x1, y1, x2, y2, err1, err0, graphicsearch_query [, screenshot, findall, joinqueries, offsetx, offsety])
functionally identicle to .scan
but uses legacy argument order as a convience for old scripts
the search scope's upper left corner coordinates
the search scope's lower right corner coordinates
A number between 0 and 1 (0.1=10%) for fault tolerance of foreground (err1) and background (err0)
GraphicsSearch queries as strings. Can be multiple queries separated by
|
if the value is 1, a new capture of the screen will be used; else it will use the last capture
if the value is 1, graphicsearch will find all matches. for 0, only return one match
if the value is 1, Join all GraphicsSearch queries for combination lookup
Set the Max offset for combination lookup
Set the direction search is conducted in
1( Left to Right / Top to Bottom )
2( Right to Left / Top to Bottom )
3( Left to Right / Bottom to Top )
4( Right to Left / Bottom to Top )
5( Top to Bottom / Left to Right )
6( Bottom to Top / Left to Right )
7( Top to Bottom / Right to Left )
8( Bottom to Top / Right to Left )
9( Center to Four Sides )
Zoom percentage of image width and height (0.1=10%)
(Array) Return an array of objects containing all lookup results, else false
if no matches were found.
Any result is an associative array {1:X, 2:Y, 3:W, 4:H, x:X+W//2, y:Y+H//2, id:tag}. All coordinates are relative to Screen, colors are in RGB format, and combination lookup must use uniform color mode
oGraphicSearch.find(x1, y1, x2, y2, err1, err0, "|<tag>*165$22.03z", 1, 1, 0, 20, 10)
; => [{1: 1215, 2: 400, 3: 22, 4: 10, id: "tag", x: 1226, y: 412}]
Sort the results object from left to right and top to bottom, ignoring slight height difference
The GraphicSearch results object to sort
The ammount of height difference to ingnore in pixels
(Array) Return an array of objects containing all lookup results
resultsObj := [ {1: 2000, 2: 2000, 3: 22, 4: 10, id: "HumanReadableTag", x: 2000, y: 2000}
, {1: 1215, 2: 400, 3: 22, 4: 10, id: "HumanReadableTag", x: 1226, y: 412}]
oGraphicSearch.resultSort(resultsObj)
; => [{1: 1215, 2: 400, 3: 22, 4: 10, id: "HumanReadableTag", x: 1226, y: 412}, {1: 2000, 2: 2000, 3: 22, 4: 10, id: "HumanReadableTag", x: 2000, y: 2000}]
Sort the results objects by distance to a given x,y coordinate. A property "distance" is added to all elements in the returned result object
The GraphicSearch results object
The x screen coordinate to measure from
The y screen coordinate to measure from
(Array) Return an array of objects containing all lookup results
resultsObj := [ {1: 2000, 2: 2000, 3: 22, 4: 10, id: "HumanReadableTag", x: 2000, y: 2000}
, {1: 1215, 2: 400, 3: 22, 4: 10, id: "HumanReadableTag", x: 1226, y: 412}]
oGraphicSearch.resultSortDistance(resultsObj, 2000, 2000)
/*
[ {1: 2000, 2: 2000, 3: 22, 4: 10, distance: "12.08", id: "HumanReadableTag", x: 2000, y: 2000}
, {1: 1215, 2: 400, 3: 22, 4: 10, distance: "1766.58", id: "HumanReadableTag", x: 1226, y: 412}]
*/
.showMatches(resultsObject [, optionsObject])
Visually display the locations of search results on the screen. It takes in a resultsObject
, and optionally an optionsObject
to customize how the results are displayed.
The GraphicSearch results object
The options object results object
No values are returned
resultsObj := [ {1: 2000, 2: 2000, 3: 22, 4: 10, id: "HumanReadableTag", x: 2000, y: 2000}
, {1: 1215, 2: 400, 3: 22, 4: 10, id: "HumanReadableTag", x: 1226, y: 412}]
oGraphicSearch.showMatches(resultsObj, {showlabels: false, timeout: 60000}}]