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
tuzi
Posts: 223
Joined: 27 Apr 2016, 23:40

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

Post by tuzi » 18 Oct 2021, 08:49

iseahound wrote:
14 Oct 2021, 11:23
Great tutorial! :bravo:

Edit: I added it to the github page. I can only speak chinese so I just copied your sentence. :lol:
You mean you can talk with chinese not spell? That's amazing!!! :thumbup: :thumbup: :thumbup:

By the way, I told everyone around me who use ahk that if you want to do anything with image, try using imageput first, and then switch to something else if it doesn't work. It works really well! :bravo:

tuzi
Posts: 223
Joined: 27 Apr 2016, 23:40

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

Post by tuzi » 18 Oct 2021, 09:07

I want to use ImagePutHIcon() to load an icon for tray menu display, but I get an error message.
Did I do something wrong?

ahk 1.1.33.10
win10 x64

Code: Select all

Menu, Tray, Add, test, handler

ico1:="iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAA4klEQVQ4T6XToU5DQRCF4e+6VvQBeIVCQuqQYCENinoMDQkCgcJQ"
ico1.="dE0FqkhEkzYoBAEErrwA8DqQSe5NNrTcbtNRK3b+nTnnbGHDKsr+AW4yWD3M0nsV4Afb+M6AnGIPZzhIAdW5jrGP9+RCa11AExMc"
ico1.="4xmHdYAjXOEaH9jFPYblBA08/Afo4A13OMcFRrhMRGyHZssAW3jFI8KdyqG/DnxiZxngBXPcrnAknCtSwEk5Xry4qjnYC4AuntDH"
ico1.="OCMPC4DcIAU7BPxKV8iNcjpYrDnISV/tNhVgihBxnYpP1dt4gl9sRC5dONyGKQAAAABJRU5ErkJggg=="

ico:=Base64PNG_to_HICON(ico1)  ; this work
; ico:=ImagePutHIcon(ico1)       ; this not work 

; show a icon in tray menu
Menu, Tray, Icon, test, HICON:%ico%

MsgBox

handler:
return

#Include ImagePut.ahk

Base64PNG_to_HICON(Base64PNG, W:=0, H:=0) {     ;   By SKAN on D094/D357 @ tiny.cc/t-36636
Local BLen:=StrLen(Base64PNG), Bin:=0,     nBytes:=Floor(StrLen(RTrim(Base64PNG,"="))*3/4)
  Return DllCall("Crypt32.dll\CryptStringToBinary", "Str",Base64PNG, "UInt",BLen, "UInt",1
            ,"Ptr",&(Bin:=VarSetCapacity(Bin,nBytes)), "UIntP",nBytes, "UInt",0, "UInt",0)
       ? DllCall("CreateIconFromResourceEx", "Ptr",&Bin, "UInt",nBytes, "Int",True, "UInt"
                 ,0x30000, "Int",W, "Int",H, "UInt",0, "UPtr") : 0            
}

iseahound
Posts: 1427
Joined: 13 Aug 2016, 21:04
Contact:

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

Post by iseahound » 18 Oct 2021, 10:56

https://github.com/iseahound/ImagePut/commit/13525ecc5b46024a844cd1359771fafcad2a2741

:headwall:

For this case, I think you should use SKAN's function, it should be faster.

Raymond Chen explains why CreateIconFromResourceEx supports PNG files. (Answer: It supports PNG compression inside ICO files, but to minimize confusion, they decided to allow PNG files as well.)
https://devblogs.microsoft.com/oldnewthing/20101022-00/?p=12473

tuzi
Posts: 223
Joined: 27 Apr 2016, 23:40

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

Post by tuzi » 18 Oct 2021, 20:00

iseahound wrote:
18 Oct 2021, 10:56
https://github.com/iseahound/ImagePut/commit/13525ecc5b46024a844cd1359771fafcad2a2741

:headwall:

For this case, I think you should use SKAN's function, it should be faster.

Raymond Chen explains why CreateIconFromResourceEx supports PNG files. (Answer: It supports PNG compression inside ICO files, but to minimize confusion, they decided to allow PNG files as well.)
https://devblogs.microsoft.com/oldnewthing/20101022-00/?p=12473
Thank you. works now.

A small suggestion, maybe change the version number to 1.2.1?

iseahound
Posts: 1427
Joined: 13 Aug 2016, 21:04
Contact:

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

Post by iseahound » 28 Dec 2021, 19:02

One thing I really want to implement is collections, which could be:
  • Pages of a PDF
  • Frames of a GIF
  • Files in a Folder or CBZ
  • Recording of a window / screen ?
But here's the issue. Not all image types support multiple images. So this is the behavior I have in mind:

Let's say the input is a collection, for example a GIF:
1) If the output supports only one image, only the first image is the output.
a) Allow the user to select which image in the collection is the output.
b) If the user says to process the entire collection, ignore the user and only output the first image in the GIF.

So here's the weird part. If the output supports a collection of images, and the input is a collection of images, always process every image by default.
So if you convert a PDF to GIF, you get an animated gif of all the pages. (weird)
And if you convert a folder to a PDF, you get a PDF of all the images in the folder (normal)

Does anyone have any ideas surrounding this topic?

iseahound
Posts: 1427
Joined: 13 Aug 2016, 21:04
Contact:

ImagePutBuffer - ImageSearch & PixelSearch

Post by iseahound » 05 May 2022, 13:07

Just added a few useful functions to ImagePutBuffer including ImageSearch, PixelSearch, SetAlpha, TransColor, and ColorKey.

ColorKey - Replaces one ARGB color with another.
SetAlpha - Sets the alpha channel of the entire image.
Transcolor - Change the transparency of one RGB color. You could make all white pixels with different transparencies the same.

Code: Select all

; Capture the screen.
pic := ImagePutBuffer(0)

; Display the image and move the cursor to the found pixel.
xy := pic.PixelSearch(0x1235FF)
ImageShow(pic)
MouseMove xy[1], xy[2]

;Imagesearch Example
xy := pic.ImageSearch("test_image.png")
ImageShow(pic)
MouseMove xy[1], xy[2]

;If you need to run image search multiple times, create a buffer for the second image so it will be faster.
test_image := ImagePutBuffer("test_image.png")
xy := pic.ImageSearch(test_image)
ImageShow(pic)
MouseMove xy[1], xy[2]



https://github.com/iseahound/ImagePut/blob/master/ImagePut%20(for%20v1).ahk

mickey12
Posts: 38
Joined: 28 Apr 2022, 19:36

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

Post by mickey12 » 20 May 2022, 01:49

Hi,
I want to convert multiple 8k Png to 4k Jpg. can somebody help me with the code as i have not much knowledge of coding I looked at the documentation and its bit confusing for me so please if anyone can help. Thanks

iseahound
Posts: 1427
Joined: 13 Aug 2016, 21:04
Contact:

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

Post by iseahound » 20 May 2022, 07:48

You just write:

imageputfile({file: "myfile.png", scale:0.5}, "output.jpg", 100) ;JPEG compression

mickey12
Posts: 38
Joined: 28 Apr 2022, 19:36

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

Post by mickey12 » 20 May 2022, 11:00

thank you that works you are great.
I have 2 question :
1. I want to give exact resolution i.e. 4096 *4096 instead of scale.

Code: Select all

imageputfile({file: "myfile.png", scale:0.5}, "output.jpg", 100) ;JPEG compression
2. When i convert transparent png image to jpg. Transparent part is replaced by black so is it possible to change that and make it white color.
But thanks anyway genius work.

iseahound
Posts: 1427
Joined: 13 Aug 2016, 21:04
Contact:

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

Post by iseahound » 20 May 2022, 12:26

imageputfile({file: "myfile.png", scale:[4096, 4096]}, "output.jpg", 100) ;JPEG compression

2. This is the first time I've seen a use case for this. You may have to run it through ImagePutHBitmap to replace the alpha color.

Code: Select all

hBitmap := ImagePutHBitmap({file: "myfile.png", scale:[4096, 4096]}, 0xFFFFFF) ; Alpha color
ImagePutFile(hBitmap, "output.jpg", 100) ;JPEG compression
ImageDestroy(hBitmap) ; Otherwise it will leak memory. 
EDIT: Oh well, #2 doesn't work properly. It could be done with DrawImage() or assembly, but it doesn't seem like a common enough use case at the moment.

mickey12
Posts: 38
Joined: 28 Apr 2022, 19:36

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

Post by mickey12 » 25 May 2022, 08:05

Hi,@iseahound
I have checked this few time #2 working for me. so when you said "#2 doesn't work properly. ". Do you find any error or quality issue in that.
Also I wonder if we have option to Increase brightness of images also?

earlbond84
Posts: 19
Joined: 16 Jul 2022, 09:26

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

Post by earlbond84 » 19 Jul 2022, 00:51

Hi @iseahound,

Any idea why ImagePutBuffer will not update the captured window while the window is inactive?

Loop{
pic := ImagePutBuffer({window: "wintitle"})
pic.show()
}

Thanks.

iseahound
Posts: 1427
Joined: 13 Aug 2016, 21:04
Contact:

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

Post by iseahound » 19 Jul 2022, 07:34

Are you seeing a visible change in the window and ImagePutBuffer is not copying those changes? If your window is occluded, it will likely not be redrawn due to power saving features (esp on windows 11)

Spitzi
Posts: 301
Joined: 24 Feb 2022, 03:45

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

Post by Spitzi » 07 Aug 2022, 02:49

Hi @iseahound

I stumbled upon ImagePut and I asked me if I could use it for my project: I have grayscale images with colored text on it, and I would like to convert the image to white with black text (to improve OCR)

my questions:
1) can that be done with ImagePut? And will it be fast?
2) how does ImagePut - ImageSearch compare to the ahk-ImageSearch regarding speed and robustness

Thanks Simon

pgarza
Posts: 4
Joined: 28 Jun 2019, 09:01

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

Post by pgarza » 09 Jan 2023, 20:59

@iseahound hello i really love this library and the work you put into it, is there anyway you can explain in simple terms how one can use your library as a replacement to built-in pixelgetcolor using variables for x,y?

Ive already figured out how to use your version of pixelsearch using screen coordinates, but im confused as to how to retrieve the color at x, y variable for further comparison.
example:

Code: Select all

PixelGetColor, OutputVar, X, Y [, Alt|Slow|RGB]
if OutputVar = color
{
	;dothis
}

iseahound
Posts: 1427
Joined: 13 Aug 2016, 21:04
Contact:

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

Post by iseahound » 10 Jan 2023, 18:12

Ah I see the problem, I never specified the exact copy and paste-able code for doing so.

Code: Select all

screen_capture := ImagePutBuffer(1) ; Capture Primary Screen
color := screen_capture[100, 200]
MsgBox % color
The docs should be much clearer now. https://github.com/iseahound/ImagePut/wiki/PixelSearch-and-ImageSearch#pixelgetcolor--pixelsetcolor

pgarza
Posts: 4
Joined: 28 Jun 2019, 09:01

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

Post by pgarza » 11 Jan 2023, 03:50

iseahound wrote:
10 Jan 2023, 18:12
Ah I see the problem, I never specified the exact copy and paste-able code for doing so.

Code: Select all

screen_capture := ImagePutBuffer(1) ; Capture Primary Screen
color := screen_capture[100, 200]
MsgBox % color
The docs should be much clearer now. https://github.com/iseahound/ImagePut/wiki/PixelSearch-and-ImageSearch#pixelgetcolor--pixelsetcolor
thank you!

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

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

Post by fiendhunter » 17 Nov 2023, 11:41

@iseahound if you can help me, it would be great.
I need to capture part of the screen as 512 color. And convert into base64 string to compare images strings in bin file.
my goal is compare and get most similar string as result in fastest way. Currently bin file has 625 image total 121MB.

I store the png files into ".bin" file with using "`n" as delimiter.

Code: Select all

str := ImagePutBase64(FileName, "png") ; Returns base64 string.

fileappend, %str% `n, %A_ScriptDir%\ABase64PNG.bin

Code: Select all

FileRead, base64Strings, %A_ScriptDir%\ABase64PNG.bin
base64Array := StrSplit(base64Strings, "`n")
The png files are 512 color range.
Color composition of 512 color is:
R-G-B;
dec : 0, 36, 72, 109, 145, 182, 218, 255
hex : 0x00, 0x24, 0x48, 0x6D, 0x91, 0xB6, 0xDA, 0xFF

Code: Select all

Gdip_PNGtoQ8PNG(thebitmap, RGBQuantize := 8) {
	pToken := Gdip_Startup()
	; Load the png with LoadPicture()
	pBitmapResource := Gdip_CreateARGBBitmapFromHBITMAP(thebitmap)
	Gdip_GetImageDimensions(pBitmapResource, wResource, hResource)
	Gdip_LockBits(pBitmapResource, 0, 0, wResource, hResource, strideResource, scanResource, bitmapDataResource)
	lookup := {1:0, 2:36, 3:72, 4:109, 5:145, 6:182, 7:218, 8:255}
	; quantization with 512 color = (Photoshop posterization by 8)
loop, % hResource
{
   y := A_Index - 1
   loop, % wResource
   {
      x := A_Index - 1
      pixelColorResource := NumGet(ScanResource+0, (A_Index-1)*4 + y*StrideResource, "uint")
	  if (pixelColorResource != 0x000000)
	  {
      Red := (pixelColorResource >> 16) & 0xFF
      Green := (pixelColorResource >> 8) & 0xFF
      Blue := pixelColorResource & 0xFF
Red := lookup[(Red // 32) + 1]
Green := lookup[(Green // 32) + 1]
Blue := lookup[(Blue // 32) + 1]
		; add 0xFF000000
	  pixelColorResource := (Red << 16) | (Green << 8) | Blue
	  pixelColorResource := pixelColorResource | 0xFF000000
      NumPut(pixelColorResource, ScanResource+0, (A_Index-1)*4 + y*StrideResource, "uint")
	  }
   }
}
	
	Gdip_UnlockBits(pBitmapResource, bitmapDataResource)
	; Save the bitmap to a file
	Gdip_SaveBitmapToFile(pBitmapResource, "C:\Q8\" thebitmapnowName ".png")
	Gdip_DisposeImage(pBitmapResource)
	Gdip_Shutdown(pToken)
	return
}

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

Post by ElVerdaderoJuan » 02 Jan 2024, 22:42

I need help :headwall: .

I create a simple GUI to test it but there are errors for show the image in the GUI.

The text is saved in "Str" correctly, therefore the error may be at the time of creating the GUI

This is the code with the error :terms:

Code: Select all

#Include <ImagePut>

Str := ImagePutBase64("Lobo.jpg")

MyGui := Gui()
Pic := MyGui.Add("Pic", "w600 h-1 +Border", "HBITMAP:*" Str)
MyGui.OnEvent("Escape", (*) => ExitApp())
MyGui.OnEvent("Close", (*) => ExitApp())
MyGui.Show

wineguy
Posts: 9
Joined: 03 Dec 2019, 16:26

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

Post by wineguy » 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.

Post Reply

Return to “Scripts and Functions (v1)”