gdip_imagesearch v2? or ImagePut? output multiple matches?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
bapl
Posts: 119
Joined: 17 Apr 2021, 00:24

gdip_imagesearch v2? or ImagePut? output multiple matches?

16 Dec 2022, 12:13

Hello!
In AHK V1, I found Gdip_ImageSearch, it distributed image search to me by hwnd.
It is also covered by the background and put on several related matches.

Code: Select all

#Include Gdip_All.ahk
#Include Gdip_ImageSearch.ahk
CoordMode Pixel, Screen

Token := Gdip_Startup()
WinGet, hwnd, ID, A
bmpHaystack := Gdip_BitmapFromHWND(hwnd) 
bmpNeedle := Gdip_CreateBitmapFromFile("image.png")
RET := Gdip_ImageSearch(bmpHaystack,bmpNeedle, outputVar,0,0,0,0,0,0xFF0000,1,90)
Gdip_DisposeImage(bmpHaystack)
Gdip_DisposeImage(bmpNeedle)
In AHK V2, I did not find the Gdip_ImageSearch library, but I did find ImagePut: viewtopic.php?t=76633
There is only one problem with ImagePut:
I don't understand how can I find multiple identical matches in a window?

Here is what I have at the moment:

Code: Select all

#Include ImagePut.ahk2
#Include Gdip_All.ahk2
ImagePut.gdiplusStartup()
hwnd := WinGetID("A")

Numpad0::
{
pBitmap := Gdip_BitmapFromHWND(hwnd)
pic := ImagePutBuffer(pBitmap)
xy := pic.ImageSearch("file.png")
if xy
MouseMove xy[1]-8, xy[2]-16
}
iseahound
Posts: 1463
Joined: 13 Aug 2016, 21:04
Contact:

Re: gdip_imagesearch v2? or ImagePut? output multiple matches?

16 Dec 2022, 13:03

Currently ImagePut returns only the first match
bapl
Posts: 119
Joined: 17 Apr 2021, 00:24

Re: gdip_imagesearch v2? or ImagePut? output multiple matches?

16 Dec 2022, 13:52

iseahound wrote:
16 Dec 2022, 13:03
Currently ImagePut returns only the first match
Have you thought about adding this?
iseahound
Posts: 1463
Joined: 13 Aug 2016, 21:04
Contact:

Re: gdip_imagesearch v2? or ImagePut? output multiple matches?

16 Dec 2022, 21:30

Yes, but no one has requested it yet. I think it would require a new function, such as array := im.ImageSearchAll(image). Send me a message in the next couple of days if nothing happens; it should be a simple addition to the library.

Also, you can do this directly without gdip_all

Code: Select all

#Include ImagePut.ahk2
#Include Gdip_All.ahk2
; CoordMode Pixel, Screen

Numpad0::
{
pic := ImagePutBuffer("A") ; anything goes in here
xy := pic.ImageSearch("file.png")
if xy
MouseMove xy[1]-8, xy[2]-16
}
iseahound
Posts: 1463
Joined: 13 Aug 2016, 21:04
Contact:

Re: gdip_imagesearch v2? or ImagePut? output multiple matches?

17 Dec 2022, 11:28

image.png
image.png (410.7 KiB) Viewed 913 times
Steps:
1. Use Win + Shift + s to open snipping tool
2. Grab an area from the screen.
3. Press 1 to show all occurrences of that area.
4. Press Esc to close all windows and reload script.

Code: Select all

#include ImagePut.ahk
#singleinstance force

1:: {
   ; Get screen.
   pic := ImagePutBuffer(0)
   search := ImagePutBuffer(ClipboardAll)

   ; Optional: Makes the top-left pixel color transparent.
   ; Doing so allows searching of non-rectangular images.
   ; If you enable this the ImageSearch will become a lot slower.
   ; search.transcolor(search[0, 0])

   ; ImageSearch
   xys := pic.imagesearchall(search)
   ;hwnd := pic.show()
   if xys {
      MsgBox "Matches Found: " xys.length
      for i, xy in xys 
      {
         ; MouseMove(xy*)
         search.show(1, i, [xy[1], xy[2]])
      }
   } else 
      search.show(1, "Not Found", [A_ScreenWidth//2 - 600])
}

Esc:: Reload
Still in testing mode so only works on AHK v2 64-bit.

https://raw.githubusercontent.com/iseahound/ImagePut/master/ImagePut.ahk (too big for the forum)
bapl
Posts: 119
Joined: 17 Apr 2021, 00:24

Re: gdip_imagesearch v2? or ImagePut? output multiple matches?

19 Dec 2022, 18:09

@iseahound
Looks delicious, thanks for your hard work!
It would also be nice to be able to specify an alpha color to ignore (for example: FF0000), as well as add an accuracy threshold if the fragment sometimes changes brightness.
iseahound
Posts: 1463
Joined: 13 Aug 2016, 21:04
Contact:

Re: gdip_imagesearch v2? or ImagePut? output multiple matches?

19 Dec 2022, 18:54

You can specify an alpha color to ignore by making colors transparent:

search.transcolor(0xFF0000)

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: jsong55, lexikos and 30 guests