Image search looping.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Smajlinger
Posts: 11
Joined: 06 Nov 2016, 16:35

Image search looping.

20 Aug 2017, 09:17

Hello everyone!

I am semi expirienced with ahk. I have done some simple scripts but now I am heading some more complicated problem and i couldnt find a good solution.

I want to make a script wchich will be loooking for images in certain area and then move items to desire destination. Problem is with looping.

After first loop if script found image all seems to be fine, but in next loop script is start from all again and that's consume a lot of time and CPU.

Could you guys look at my code below and give me some tips how can I improve performance of my script?

Also is it possible to not see my moving cursor at screen during script is working?

Thank you guys in advance!

Code: Select all

insert::
SetBatchLines, -1

MouseGetPos, x1, y1
Loop 40

{
ImageSearch, pX, pY, 1570, 300, 1745, 815, *2, C:\Users\Sylwek\Desktop\loot_mary\item%a_index%.png
if ErrorLevel = 0
   {
	
       	SetDefaultMouseSpeed, 0
    	MouseClickDrag, Left, %pX%, %pY%, %InvX%, %Invy%
	sleep, 250
	
	
   }

}
MouseMove, %x1%, %y1%

return
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: Image search looping.

20 Aug 2017, 17:53

Make a sleep between searches (at the end of the loop) I guess?
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
okl

Re: Image search looping.

21 Aug 2017, 12:28

honestly, i think that's about as fast as a loop can get... but that's just me.
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Image search looping.

21 Aug 2017, 12:52

You might see some performance increase if you use GDIP image search instead of the built in image search, it's definitely significantly faster.
Smajlinger
Posts: 11
Joined: 06 Nov 2016, 16:35

Re: Image search looping.

22 Aug 2017, 04:24

MaxAstro wrote:You might see some performance increase if you use GDIP image search instead of the built in image search, it's definitely significantly faster.
Thank you guys for feedback.

@MaxAstro;
Could you please say something more about this? Some reference script will be perfect.

Thank you in adavance!
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Image search looping.

22 Aug 2017, 11:13

The original links are a bit hard to track down, so I've attached the AHK GDIP library files to this post. They are a bit complicated to use (read the comments carefully), but lightning fast. The first thing you'll want to do is use this to initialize GDIP:

Code: Select all

if !pToken := Gdip_Startup()	; Start GDI+ for advanced image handling
{
	MsgBox, 48, % "GDI+ error!", % "GDI+ failed to start. Please ensure you have gdiplus on your system!"
	return
}
At that point, the functions you will most likely need are Gdip_BitmapFromHWND, which will create a GDIP bitmap from the specified hWND (basically a "screenshot" of whatever window you want to search), Gdip_CreateBitmapFromFile, which does the same thing for a specified file, and Gdip_ImageSearch, which will search one bitmap for the presence of another. An example would look like this:

Code: Select all

NeedlePath := "C:\MyImage.png"
pNeedle := Gdip_CreateBitmapFromFile(NeedlePath)	; Create the needle from the chosen file
pHwnd := WinExist("A")								; Get the hWND of the active window
pHaystack := Gdip_BitmapFromHWND(pHwnd)				; Create the haystack from the active window
Gdip_ImageSearch(pHaystack, pNeedle, ImageCoords)	; Search the haystack for the needle
if (ImageCoords)
	MsgBox % "The image was found at coordinates " . ImageCoords
else
	MsgBox % "Image not found!"
This code searches the active window for an image that matches the contents of a file named MyImage.png in the root of C. Hopefully you can edit that to do what you need. :)

At the end of your script you'll want to clean up any bitmaps you've created by calling Gdip_DisposeImage, and then shut down GDIP with Gdip_Shutdown. For the above script, that would look like this:

Code: Select all

Gdip_DisposeImage(pNeedle)
Gdip_DisposeImage(pHaystack)
Gdip_Shutdown(pToken)
Attachments
Gdip_ImageSearch.ahk
(32.59 KiB) Downloaded 31 times
Gdip_All.ahk
(95.45 KiB) Downloaded 16 times

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, Sniperman and 359 guests