How to get a script to run when an image is detected?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Yungin
Posts: 1
Joined: 23 Apr 2017, 03:15

How to get a script to run when an image is detected?

23 Apr 2017, 03:23

Hey!

I'm not that great in writing AHK scripts myself so if somebody would be able to help me with this, that would be great.

What I want the script to do:

Do a line of script (for example MouseMoves and Clicks) which I have already written, only when the script detects an image such as this:

Image

Huge thanks in advance!
Rohwedder
Posts: 7647
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to get a script to run when an image is detected?

23 Apr 2017, 11:47

Hallo,
If the form is important, not just the color: black, then I see only one possibility.
https://autohotkey.com/docs/commands/ImageSearch.htm
CodilX
Posts: 3
Joined: 20 Apr 2017, 07:53

Re: How to get a script to run when an image is detected?

26 Apr 2017, 00:55

I made a simple bot that played a game once, it worked fine-ish, it was a quick and dirty way of making the bot, but it acted predictably and did what I wanted so.. job well done.

First I just made a loop firing every 500ms.

I knew where specific elements would appear on screen so I knew the coordinates (x & y). With nircmdc you can grab a screenshot AND specify specific coordinates and desired image size to immediately crop it.

Code: Select all

nircmdc.exe savescreenshot images/temp.jpg 28 33 359 638
(28 being the X offset, 33 the Y offset, 359 & 638 the size of the cropped image)

Initiate that command manually once the target image/element is in view (when you want some code to trigger) and save that image for later use. Put the same command inside your loop.

Inside the loop use another external command to compare the captured live image with the one you saved earlier with ImageMagicks compare feature.

Code: Select all

compare.exe -verbose -metric  mse "images/temp.jpg" "images/previouslySavedImage.jpg" null
Once the images are compared you get a number value indicating how similar those images are. The lower the number - the more similar they are. You have to play with this threshold, for some images you might find < 500 is good, for others < 3000 and etc. When you find a threshold that can be used to reliably differentiate the image you want, just add an if statement and the commands you want to execute.

You can extend this to find multiple elements on screen and compare it with known elements you have saved previously. Thus the script can look for multiple things you might want to be automated and react accordingly. But this process is CPU intensive, the more screenshots (+ crops) and comparing you do - the greater toll on the system.

Buuuut it works and is responsive enough to play a dumb game on its own.

Edit: you can do the same thing I believe with the ImageSearch command, for certain reasons I had to use these external tools myself, but the idea remains the same.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, NinjoOnline and 242 guests