Giter VIP home page Giter VIP logo

graphicsearch.ahk's Introduction

Long-form README: https://chunjee.github.io/graphicsearch.ahk

GraphicSearch

A fast, super powerful, and flexible screen searching library for AHK

What is it?

Can be thought of as an alternative to native AHK Imagesearch command. The native command requires saved image files, nearly identical image matching, can be difficult to troubleshoot, and performs in a relatively slow manner. GraphicSearch approaches searching differently. Think of ASCII art; GraphicSearch abstracts the screen's image into representative 0's and _'s. Because this is an abstraction, not a bit-for-bit comparison, it allows for faster matching and easier adjustments of fault tolerance. It can also check for several different graphics without recapturing the screen's image every time. Perhaps most useful, it can return all matches unlike AHK ImageSearch which only returns the first match.

Installation

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.

Main Methods

.search(graphicsearch_query [, options])

finds GraphicSearch queries on the screen

Arguments

graphicsearch_query (string)

The GraphicSearch query(s) to search. Must be concatinated with | if searching multiple graphics

[options:={}] (object)

The options object

[options.x1:=0] (number), [options.y1:=0] (number)

the search scope's upper left corner coordinates

[options.x2:=A_ScreenWidth] (number), [options.y2:=A_ScreenHeight] (number)

the search scope's lower right corner coordinates

[options.err1:=1] (number), [options.err0:=0] (number)

Fault tolerance of graphic and background (0.1=10%)

[options.screenshot:=1] (boolean)

Wether or not to capture a new screenshot or not. If the value is 0, the last captured screenhhot will be used

[options.findall:=1] (boolean)

Wether or not to find all instances or just one.

[options.joinqueries:=1] (boolean)

Join all GraphicsSearch queries for combination lookup.

[options.offsetx:=1] (number), [options.offsety:=0] (number)

Set the Max offset for combination lookup

Return

(Array) Return an array of objects containing all lookup results, else false if no matches were found.

Example

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

.searchAgain([graphicsearch_query])

performs the last .search with the last arguments supplied

Example

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

Arguments

graphicsearch_query (string)

GraphicsSearch queries as strings. Can be multiple queries separated by |

[x1:=0, y1:=0] (number)

the search scope's upper left corner coordinates

[x2:=0, y2:=0] (number)

the search scope's lower right corner coordinates

[err1:=0, err0:=0] (number)

A number between 0 and 1 (0.1=10%) for fault tolerance of foreground (err1) and background (err0)

[screenshot:=1] (boolean)

if the value is 1, a new capture of the screen will be used; else it will use the last capture

[findall:=1] (boolean)

if the value is 1, graphicsearch will find all matches. for 0, only return one match

[joinqueries:=1] (boolean)

if the value is 1, Join all GraphicsSearch queries for combination lookup

[offsetx:=0, offsety:=0] (number)

Set the Max offset for combination lookup

Return

(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

Example

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

Example

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

Arguments

x1, y1 (number)

the search scope's upper left corner coordinates

x2, y2 (number)

the search scope's lower right corner coordinates

err1, err0 (number)

A number between 0 and 1 (0.1=10%) for fault tolerance of foreground (err1) and background (err0)

graphicsearch_query (string)

GraphicsSearch queries as strings. Can be multiple queries separated by |

[screenshot:=1] (boolean)

if the value is 1, a new capture of the screen will be used; else it will use the last capture

[findall:=1] (boolean)

if the value is 1, graphicsearch will find all matches. for 0, only return one match

[joinqueries:=1] (boolean)

if the value is 1, Join all GraphicsSearch queries for combination lookup

[offsetx:=20, offsety:=10] (number)

Set the Max offset for combination lookup

[direction:=1] (number)

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 )

[zoomW:=1, zoomH:=1] (number)

Zoom percentage of image width and height (0.1=10%)

Return

(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

Example

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}]

Sorting Methods

.resultSort(resultsObject[, ydistance])

Sort the results object from left to right and top to bottom, ignoring slight height difference

Arguments

[resultsobject] (Object)

The GraphicSearch results object to sort

[ydistance:=10] (number)

The ammount of height difference to ingnore in pixels

Return

(Array) Return an array of objects containing all lookup results

Example

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}]

.resultSortDistance(resultsObject [, x, y])

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

Arguments

resultsObject (Object)

The GraphicSearch results object

[x:=1] (number)

The x screen coordinate to measure from

[y:=1] (number)

The y screen coordinate to measure from

Return

(Array) Return an array of objects containing all lookup results

Example

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}]
*/

graphicsearch.ahk's People

Contributors

chunjee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

graphicsearch.ahk's Issues

Change capture hotkey / button

Can you make the image capture hotkey (or mouse button) changeable?

I hacked my copy of your script to use ScrollLock instead of RButton, because RButton changes the state of the application I was trying to automate (removing the image I was trying to capture!)

(Incidentally, I don't understand the GUI's hotkey settings, and don't see any mention of it in the documentation, but haven't read it all yet...)

Also, this is how I would prefer capture to work:
Hotkey to start capture and open capture box.
Trap all keystrokes and mouse movements during capture
Arrows to resize capture box
Hotkey to stop capture.

Alternatively, the hotkey that starts capture could capture the entire screen of all monitors, then the bounding-box could be navigated over that entire captured image to find the correct location to clip. (But that seems a bit more complicated.)

AHK v2 support

Does this work with AHK v2? Or does it need to be upgraded?

BindWindow() doesn't seem to work

First I'd like to thank you so much for the awesome work with this library!
Second, the function BindWindow() seems to fail when a window is on top of the search area(obscured).
If you're on AHK discord channel we can screen-share to show you what I mean exactly.

Thank you very much once again!

cheers,
yawik

5th Pizza Not Found

Thank you for adapting the FindText code and providing documentation. When testing, it only found the four pizzas inside the white circle. It wasn't able to find the lower right pizza in the black background. For the image, I used a small square area inside the pizza. What am I missing?

Query about joinqueries

Can we have a bit more detailed information about the usage of joinqueries? Normally we already join multiple queries with pipes (|) at the beginning of each query generated by the GUI. It works like OR operation between the given queries joined by pipes. How does joinqueries influence this? Does it support joints of multiple groups of queries and/or logical operations like (q1 | q2 | q3 ) & (q4 | q5). Detailing this will be very helpful and much appreciated.

Also, are there any usage tips to minimize the anti-aliasing differences between captures of the same object from different areas of the screen. Normally it takes quite a few variations of the same capture to reliably detect a moving object.

Coordinates returned for wrong screen (but not when using provided GUI test)

Hi,
I've run into a what appears to be a bug that I can't track down, but is still reproducible when searching on a 2 monitor setup. In this example, two monitors both on 1920x1080 resolution, side by side. So coordinates range from -1920,0 to 1920,1080

I'm getting the coordinates of the found images all returned for the wrong screen.
So for example, if the image is actually at x=10|y=20, the returned coords from graphicsearch are x=-1910|y=20

The strange part is that it works fine when using the "graphicsearch_gui.ahk" test function.

Steps to reproduce the issue are as follows:

  • Have 2 monitors with right monitor set to main monitor
  • Open graphicsearch_gui.ahk
  • Click capture, setup the greyscale, click ok
  • Click Test while the image is on the main monitor (this spits out the correct coordinates on and highlights the images correctly on the correct screen)
  • Copy the supplied code into a new ahk document and run it. This will spit out the incorrect coordinates (in my case, all X values are off by a screenwidth. Worse still is that it will not find the image at all if it's on the left monitor.

I've tried for hours to work out whats different between the gui test function and the actual library, but a lot of it goes above my head so I've had no luck.

feature request: a function instead of gui to convert existing image files to queries

Interesting library! I would like to try this instead of the ImageSearch in Gdip. I have many images already saved as files. Is there a way to convert them to queries automatically?

Also, it seems that the code only searches the current screen. Is it possible to search a image in memory? e.g. in the Gdip library I have a pointer pointing to bitmap object which contains the current screenshot of a game.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.