ImagePut - A core library for images in AutoHotkey (Now supports HEIC & WEBP)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
ElVerdaderoJuan
Posts: 8
Joined: 14 Dec 2023, 15:04
Contact:

Re: ImagePut - Image library for converting to files, streams, windows, base64, urls, cursors, screen coordinates, clipb

03 Jan 2024, 19:27

wineguy wrote:
03 Jan 2024, 03:41
@ElVerdaderoJuan - I don't actually use ImagePut, but HBITMAP requires a handle, not Base64 Text as you have supplied.

According to ImagePut Wiki, you probably want ImagePutHBitmap instead of ImagePutBase64.
That worked, thank you very much! :xmas:
iseahound
Posts: 1451
Joined: 13 Aug 2016, 21:04
Contact:

Re: ImagePut - Image library for converting to files, streams, windows, base64, urls, cursors, screen coordinates, clipb

26 Jan 2024, 18:03

I added a new wiki page that shows how to integrate ImagePut to the AutoHotkey GUI. Note: The GUI code posted earlier in this forum is for AutoHotkey v2. Likewise, the following code will only address AutoHotkey v2.

https://github.com/iseahound/ImagePut/wiki/Add-Image-to-AutoHotkey-GUI
fiendhunter
Posts: 140
Joined: 24 Jul 2019, 15:27

Re: ImagePut - A core library for images in AutoHotkey (Now supports HEIC & WEBP)

25 Apr 2024, 21:07

Code: Select all

;@Ahk2Exe-AddResource C:\T1_Test_Image .bmp
;@Ahk2Exe-IgnoreBegin
HT1_Test_Image := LoadPicture("C:\T1_Test_Image.bmp")
;@Ahk2Exe-IgnoreEnd
HT1_Test_Image := LoadBitmapResource("T1_Test_Image.bmp")


; tried to convert gdi+	pT1_Test_Image := Gdip_CreateBitmapFromHBITMAP(HT1_Test_Image)
buf := ImagePutBuffer([0, 0, 1920, 972, "A"])
if xy := buf.ImageSearch(HT1_Test_Image) {           ; Search image working with buf.ImageSearch("C:\T1_Test_Image.bmp")
{
MouseMove xy[1], xy[2]
}
return
I couldnt figure how to search a HBitmap out. Did I miss someting? Could you show me the path please?
iseahound
Posts: 1451
Joined: 13 Aug 2016, 21:04
Contact:

Re: ImagePut - A core library for images in AutoHotkey (Now supports HEIC & WEBP)

26 Apr 2024, 00:17

Does your image include transparency? If so HBitmap does not support transparency,

Try this script which searches an HBitmap:

Code: Select all

#include ImagePut (for v1).ahk
#singleinstance force

; Get a random image.
pic := ImagePutBuffer("https://picsum.photos/720")

; Get random coordinates.
Random, x, 0, % pic.width
loop
   Random, x2, 0, % pic.width
until x2 != x
Random, y, 0, % pic.height
loop
   Random, y2, 0, % pic.height
until y2 != y

; Crop the source image to create a random search image.
search := pic.crop(min(x, x2), min(y, y2), abs(x2 - x), abs(y2 - y))
search2 := ImagePutHBitmap(search)


; Optional: Make all white pixels transparent.
; Doing so allows searching of non-rectangular images.
; search.transcolor(0xFFFFFF)

; Find the cropped image.
xy := pic.imagesearch(search2)
hwnd := pic.show()

; If found, create a box around it.
if (xy) 
{
   MouseMove xy[1], xy[2]
   WinGetPos wx, wy,,, % "ahk_id" hwnd
   search.show(1, "", [wx+xy[1], wy+xy[2]])
}
else 
   search.show(1, "Not Found", [A_ScreenWidth//2 - 600])


+Esc:: Reload
Esc:: ExitApp
fiendhunter
Posts: 140
Joined: 24 Jul 2019, 15:27

Re: ImagePut - A core library for images in AutoHotkey (Now supports HEIC & WEBP)

26 Apr 2024, 02:36

@iseahound
Thank for reply. My test image does not include transparent pixels. but will also use transparent images.

I need a HbitmapPutBuffer() function. At your example ImagePutHBitmap() it converts an image into hbitmap and you can use this for pic.imagesearch()

But when I tried to add an hbitmap directly into pic.imagesearch() it doesnt find any match. "buf.ImageSearch(HT1_Test_Image)"

LoadBitmapResource() (.exe) and LoadPicture() (.ahk) are same hbitmap output. What is the different with your ImagePutHBitmap()?

Code: Select all

LoadBitmapResource(resName, shared := true) ; not support PNG
{
	resNameType := "str"
	if resName is integer
		if resName between 0 and 0xFFFF
			resNameType := "ptr"
	ret := DllCall("LoadImage", "ptr", GetOwnModuleHandle(), resNameType, resName, "uint", 0, "int", 0, "int", 0, "uint", shared ? 0x8000 : 0, "ptr")
	return ret
}

GetOwnModuleHandle()
{
	static h := DllCall("GetModuleHandle", "ptr", 0, "ptr")
	return h
}
}
iseahound
Posts: 1451
Joined: 13 Aug 2016, 21:04
Contact:

Re: ImagePut - A core library for images in AutoHotkey (Now supports HEIC & WEBP)

27 Apr 2024, 03:24

I could not find any errors:

Code: Select all

#include ImagePut (for v1).ahk
HT1_Test_Image := LoadPicture("a.bmp")

buf := ImagePutBuffer(0) ; 0 means all screens
if xy := buf.ImageSearch(HT1_Test_Image) {
   MouseMove xy[1], xy[2]
}
return
ImagePutHBitmap is part of my collection of customized and optimized functions. It is better than LoadPicture because it preserves transparency.
fiendhunter
Posts: 140
Joined: 24 Jul 2019, 15:27

Re: ImagePut - A core library for images in AutoHotkey (Now supports HEIC & WEBP)

27 Apr 2024, 10:08

iseahound wrote:
27 Apr 2024, 03:24

Code: Select all

#include ImagePut (for v1).ahk
HT1_Test_Image := LoadPicture("a.bmp")

buf := ImagePutBuffer(0) ; 0 means all screens
if xy := buf.ImageSearch(HT1_Test_Image) {
   MouseMove xy[1], xy[2]
}
return
I dont know why. but when I using this "LoadPicture" does not found the target. ( with no errors )


But how "dumAmI" :) I thought that I need to fix "ImageSearch" part. No need! As your mention, replacing LoadPicture with ImagePutHBitmap fixes the issue. Now its working fine.

Storing xy coorinates not related with active window. should I do the math by myself or is there any trick?

Code: Select all

buf := ImagePutBuffer([0, 200, 1920, 972, "A"])
if xy := buf.ImageSearch(HT1_Test_Image) {
MouseMove xy[1], xy[2]
}
( CoorMode,,window )
Actual xy: 974,933 of ( HT1_Test_Image )
Stored xy: 966,702 ( buf.ImageSearch ) ; total "+8 pixel for x coord" and "+200+31 pixel for y coord" margin of deviation
iseahound
Posts: 1451
Joined: 13 Aug 2016, 21:04
Contact:

Re: ImagePut - A core library for images in AutoHotkey (Now supports HEIC & WEBP)

27 Apr 2024, 11:38

Ah. Try using CoordMode, Mouse, Client. Window capture ignores the non-client area (title bar, captions, and shadows) because there are better ways to retrieve that information.

As for your LoadPicture issue: It works on my end, and I am curious to see why it does not work on yours. If you'd like you can send me your images, and I'll see if I can reproduce it. If you have Gdip_All.ahk you can also test Gdip_CreateARGBHBITMAPFromBitmap which is basically my ImagePutHBitmap.
fiendhunter
Posts: 140
Joined: 24 Jul 2019, 15:27

Re: ImagePut - A core library for images in AutoHotkey (Now supports HEIC & WEBP)

27 Apr 2024, 14:40

Code: Select all

HT1_Test_Image1 := ImagePutHBitmap("C:\T1_Test_Image1.bmp")
HT1_Test_Image2 := ImagePutHBitmap("C:\T1_Test_Image2.bmp")
.....ETC.

pCheckList = HT1_Test_Image1,HT1_Test_Image2,HT1_Test_Image3,HT1_Test_Image4,HT1_Test_Image5,HT1_Test_Image6,HT1_Test_Image7,HT1_Test_Image8,HT1_Test_Image9

buf := ""
buf := ImagePutBuffer([0, 0, 1920, 972, "A"])
Loop, parse, pCheckList, `,
{
HT1_Selected := %A_LoopField%
HT1_Selected.transcolor(0xFFFFFF)
if yivXY := buf.ImageSearch(HT1_Selected) {
tooltip % "found by imageput " A_LoopField
sleep 3000
}
ImageSearch, yivX, yivY, 0,0, 1920, 972, *TransWhite *1 HBITMAP:*%HT1_Selected%
if (ErrorLevel = 0) {
tooltip % "found by build-in " A_LoopField
sleep 3000
}

/* ; cheking area. is image not broken. All checked has no visual problem.
bufwindow := ImageShow(HT1_Selected)
sleep 3000
ImageDestroy(bufwindow)
sleep 500
*/
}
Result if test image has no transcolor, found. Else it doesnt found.
if I use build-in imagesearch, all found. (ImageSearch, yivX, yivY, 0,0, 1920, 972, *TransWhite *1 HBITMAP:*%HT1_Selected%)

I have imageput.ahk updated 2 days ago and Gdip_all.ahk v1.96

I also tried transcolor with imageputbuffer

Code: Select all

bufferImage := ImagePutBuffer("C:\T1_Test_Image2.bmp")
bufferImage.TransColor(0xFFFFFF)
HT1_Test_Image2 := ImagePutHBitmap(bufferImage)
; bufwindow := ImageShow(HT1_Test_Image2) ; it shows image as transparent version but still it doesnt find the result.

buf := ""
buf := ImagePutBuffer([0, 0, 1920, 972, "A"])
if yivXY := buf.ImageSearch(HT1_Test_Image2) {
tooltip % "found by imageput " "HT1_Test_Image2"
}



fiendhunter
Posts: 140
Joined: 24 Jul 2019, 15:27

Re: ImagePut - A core library for images in AutoHotkey (Now supports HEIC & WEBP)

29 Apr 2024, 04:00

RESULT IS: FOUND

Code: Select all

#include ImagePut (for v1).ahk
#singleinstance force

; Get a random image.
pic := ImagePutBuffer("C:\Macrolarım\aaBitmaplib\T1_Test_Image2.bmp")

; Get random coordinates.
Random, x, 0, % pic.width
loop
   Random, x2, 0, % pic.width
until x2 != x
Random, y, 0, % pic.height
loop
   Random, y2, 0, % pic.height
until y2 != y

; Crop the source image to create a random search image.
search := pic.crop(min(x, x2), min(y, y2), abs(x2 - x), abs(y2 - y))

; Optional: Make all white pixels transparent.
; Doing so allows searching of non-rectangular images.
; search.transcolor(0xFFFFFF) ; if activate this line, and if image has transcolor, RESULT IS NOT FOUND

search2 := ImagePutHBitmap(search)



; Find the cropped image.
xy := pic.imagesearch(search2)
; hwnd := pic.show()

; If found, create a box around it. RESULT IS FOUND
if (xy) 
{
msgbox % "yes"
   MouseMove xy[1], xy[2]
   WinGetPos wx, wy,,, % "ahk_id" hwnd
   search.show(1, "", [wx+xy[1], wy+xy[2]])
}else{
msgbox % "no"
   search.show(1, "Not Found", [A_ScreenWidth//2 - 600])
}
return
if search2 has transcolor RESULT IS: NOT FOUND else RESULT IS: FOUND

Code: Select all

#include ImagePut (for v1).ahk
#singleinstance force

; Get a random image.
pic := ImagePutBuffer("C:\Macrolarım\aaBitmaplib\T1_Test_Image2.bmp")

; Get random coordinates.
Random, x, 0, % pic.width
loop
   Random, x2, 0, % pic.width
until x2 != x
Random, y, 0, % pic.height
loop
   Random, y2, 0, % pic.height
until y2 != y

; Crop the source image to create a random search image.
search := pic.crop(min(x, x2), min(y, y2), abs(x2 - x), abs(y2 - y))

; Optional: Make all white pixels transparent.
; Doing so allows searching of non-rectangular images.
search.transcolor(0xFFFFFF)

search2 := ImagePutHBitmap(search)

; get screenshot
pic := ImagePutBuffer([0, 0, 1920, 972, "A"])

; Find the cropped image.
xy := pic.imagesearch(search2)
; hwnd := pic.show()

; If found, create a box around it. if search2 has transcolor RESULT IS NOT FOUND else RESULT IS FOUND
if (xy) 
{
msgbox % "yes"
   MouseMove xy[1], xy[2]
   WinGetPos wx, wy,,, % "ahk_id" hwnd
   search.show(1, "", [wx+xy[1], wy+xy[2]])
}else{
msgbox % "no"
   search.show(1, "Not Found", [A_ScreenWidth//2 - 600])
}
return
conclusion; imagesearch doesnt work work with (if) search.transcolor(0xFFFFFF)
All my test, I couldnt manage to find any transcolored image.
iseahound
Posts: 1451
Joined: 13 Aug 2016, 21:04
Contact:

Re: ImagePut - A core library for images in AutoHotkey (Now supports HEIC & WEBP)

29 Apr 2024, 13:45

Some changes were made at the beginning of the year to add variation to image search, which removed the TransColor searching functionality. Wasn't sure if anyone actually used this, so I'll give it another pass in editing the C code.

https://github.com/iseahound/ImagePut/issues/48
fiendhunter
Posts: 140
Joined: 24 Jul 2019, 15:27

Re: ImagePut - A core library for images in AutoHotkey (Now supports HEIC & WEBP)

29 Apr 2024, 15:53

iseahound wrote:
29 Apr 2024, 13:45
Some changes were made at the beginning of the year to add variation to image search, which removed the TransColor searching functionality. Wasn't sure if anyone actually used this, so I'll give it another pass in editing the C code.

https://github.com/iseahound/ImagePut/issues/48
Thank you it will be huge improvement for my project. Build-in imagesearch takes screenshot for each search. It cost too much unnecessary power and time consumption. Your imagesearch, at my project for 100 loop makes around x12 times faster result.
iseahound
Posts: 1451
Joined: 13 Aug 2016, 21:04
Contact:

Re: ImagePut - A core library for images in AutoHotkey (Now supports HEIC & WEBP)

29 Apr 2024, 18:41

Thanks for the complement. Currently the pixelsearch module is 100% complete including SSE2 vectorization (makes it 3x faster by loading pixels 4 at a time). I'll work on releasing the ImageSearch without any vectorization to just get the transcolor working. I think I mentioned this earlier, but this is really something I work on in my spare time as I'm not automating any games or GUIs at all. I'll see if I can add some data science concepts to the project so I can simultaneously learn something new. Hmm...

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 51 guests