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

Post your working scripts, libraries and tools.
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

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

29 May 2020, 15:23

ImagePut
A core library for images in AutoHotkey

image.png
image.png (76.28 KiB) Viewed 898 times

Imagine a scenario where you have a file but you're trying to get it uploaded into an API. Or you have a function that returns some strange image format, is there any way you can *just* get it to show its contents? If you pass anything, literally anything you believe is an image to ImagePutWindow() it will show up on screen. And then you can magically transform it to be compatible with that specific API.

Features
  • Simple and effective
  • Decipher over 20+ image types
  • Supports newer formats webp and heic
  • View and debug your image functions with ImagePutWindow(image)
  • Screen capture with fast PixelSearch and ImageSearch
  • Add animated GIFs to AutoHotkey GUIs
  • Upload images using website apis
  • Load images from blocks of memory

Beginner

Intermediate

Documentation
Tutorials Misc
Advanced
So you want to convert an image?
But you don't know how. That's okay because you can just do this:

Code: Select all

str := ImagePutBase64("cats.jpg")
or this

Code: Select all

ImagePutClipboard("https://example.com/cats.jpg")
or something like this:

Code: Select all

pStream := ImagePutStream([0, 0, A_ScreenWidth, A_ScreenHeight])
Working with images should be this easy. ImagePut has automatic type inference, meaning that it will guess whether the input is (1) a file (2) a website url or (3) a series of coordinates that map to the screen. This functionality enables the user to only memorize a single function for any possible input. For a full list of supported input types, click on the documentation link here. For output types click here.

Convert file formats.

Code: Select all

; Saves a JPEG as a GIF.
ImagePutFile("cats.jpg", "gif")
Convert file formats and image types at the same time!

Code: Select all

; Saves a JPEG as a base64 encoded GIF.
str := ImagePutBase64("cats.jpg", "gif")
There's also some weird functions like ImagePutCursor which lets you set anything as your cursor. Make sure you don't choose an extremely large image!

Finally, there are several advanced features. The first is the ability to specify the input type directly. The second is cropping and scaling functionality. Third is use of ImageEqual() a function that can compare multiple inputs across different windows image data types!

Code: Select all

; Declare input type as file.
ImagePutWindow({file: "cats.jpg"})

; Scale 2x and crop 10% from each edge.
ImagePutWindow({file: "cats.jpg", scale: 2, crop:["-10%", "-10%", "-10%", "-10%"]})

; Unknown image type declared as "image" to be cropped to 200x200 pixels.
ImagePutWindow({image: "cats.jpg", crop: [0, 0, 200, 200]})

; Compare a url to a file.
MsgBox % ImageEqual("https://example.com/cats.jpg", "cats.jpg")

; Validate an image as an actual image.
ImageEqual("cats.jpg")

Design Philosophy
  • 100% Compatibility with Gdip_All.ahk
  • ImagePut is designed to be the fastest
  • ImagePut should serve as a reference implementation
  • Specific conversions between formats like PNG file to hIcon are not considered
  • Users should be able to replace uses of ImagePut with individual functions
  • Therefore users should be copy and paste individual functions
  • If you need help extracting a function please ask!

Help and Support

Feel free to ask for any help, questions, or post suggestions, etc.
Download
Last edited by iseahound on 05 Feb 2024, 15:37, edited 43 times in total.
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: [a210] ImagePut - Puts an image from anywhere to anywhere

06 Jun 2020, 13:10

a109 - AutoHotkey v2 only version.
Spoiler

a110 compatible.
v1 - With fixes suggested by Helgef and swagfag
Spoiler
Last edited by iseahound on 07 Oct 2021, 18:33, edited 7 times in total.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: [a210] ImagePut - Puts an image from anywhere to anywhere

06 Jun 2020, 13:30

Lines, 391, 393,not compatible. 1039, use char count. 1143, use ptr prop, not &. Note, you use the deprecated numput parameters.

I'll try this when I get to a PC.

Thanks for sharing, cheers.

Edit, that was relating to the second post.
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: [a110] ImagePut - Puts an image from anywhere to anywhere

07 Jun 2020, 15:07

Thanks guest3456.

@Helgef
So VarSetStrCapacity now uses wide chars?

Also I can't make heads or tails translating this to v2.

Code: Select all

         buffer := {__New: ObjBindMethod(this, "gdiplusStartup") ; Increment GDI+ reference count
               , __Delete: ObjBindMethod(this, "gdiplusShutdown", "smart_pointer", pBitmap)}
         buffer := new buffer      ; On deletion the buffer object will dispose of the bitmap.
         buffer.pBitmap := pBitmap ; And it will decrement this.gdiplus.
I'm trying to:

1) Create a simple object with __New() and __Delete() methods that are copies of existing ones.
2) Instantiate the object.
3) Add a single property.

Alternatively, it is possible to manually construct an object with a property, and two meta-methods in the base, and call the __New() meta method once.

I asked the question here. https://www.autohotkey.com/boards/viewtopic.php?f=82&t=77021&p=334250#p334250
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: [a110] ImagePut - Puts an image from anywhere to anywhere

07 Jun 2020, 19:09

Code: Select all

Buffer := {}
Buffer.DefineMethod('New', (_this, bmp) => (
	this.gdiplusStartup(),
	_this.pBitmap := bmp,
	_this
))
Buffer.DefineMethod('__Delete', _this => this.gdiplusShutdown('smart_pointer', _this.pBitmap))

B := Buffer.New(pBitmap)
like this i guess if its meant to be defined in the context of some object's method
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: [a110] ImagePut - Puts an image from anywhere to anywhere

07 Jun 2020, 19:46

Yes! This is what I ended up using:

Code: Select all

         buffer := {}.DefineMethod("__New" , (self) => (this.gdiplusStartup(), self)) ; Increment GDI+ reference count
                     .DefineMethod("__Delete", (self) => (this.gdiplusShutdown("smart_pointer", self.pBitmap)))
         buffer := buffer.__New()  ; On deletion the buffer object will dispose of the bitmap.
         buffer.pBitmap := pBitmap ; And it will decrement this.gdiplus.
It's too bad objects are so nasty in v1 and v2. Easy to use, horrible to construct.

EDIT: This looks better

Code: Select all

         buffer := {pBitmap: pBitmap}
            .DefineMethod("__New" , (self) => (this.gdiplusStartup(), self)) ; Increment GDI+ reference count
            .DefineMethod("__Delete", (self) => (this.gdiplusShutdown("smart_pointer", self.pBitmap)))
            .__New()  ; On deletion the buffer object will dispose of the bitmap. And it will decrement this.gdiplus.
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: [a110] ImagePut - Puts an image from anywhere to anywhere

07 Jun 2020, 19:58

Why are you calling __new? I didn’t think that was meant to be called directly
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: [a110] ImagePut - Puts an image from anywhere to anywhere

08 Jun 2020, 07:18

VarSetStrCapacity wrote: Specify for RequestedCapacity the number of characters that the variable should be able to hold after the adjustment. RequestedCapacity does not include the internal zero terminator. For example, specifying 1 would allow the variable to hold up to one character in addition to its internal terminator.
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

[a111] ImagePut - Puts an image from anywhere to anywhere

12 Jun 2020, 11:02

a111 compatable
Spoiler
will be away
Last edited by iseahound on 07 Oct 2021, 18:32, edited 1 time in total.
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: [a118] ImagePut - Puts an image from anywhere to anywhere

25 Jul 2020, 18:29

a118 compatible
Spoiler
  • Refactored pBitmap -> base64 conversion
  • Refactored pBitmap -> file conversion
  • When ImagePutFile() is called without a filename, the current date and time is used instead.
  • ImagePutStream() has been added. Returns a pointer to IStream.
  • ImagePutRandomAccessStream() has been added. Returns a pointer to IRandomAccessStream.
  • Monitors now work properly! ImagePutFile(1) will retrieve monitor 1, ImagePutFile(2) will retrieve monitor 2...
  • Better error messages, and removed some comments.
Last edited by iseahound on 07 Oct 2021, 18:31, edited 1 time in total.
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: [a119] ImagePut - Puts an image from anywhere to anywhere

27 Jul 2020, 16:00

a119 compatible
Spoiler
  • Pass ClipboardAll() to read from the clipboard. Ex. ImagePutFile(ClipboardAll())
  • Clipboard now supports transparency by using a PNG stream!
  • Some routines will now happen 6 times before throwing an error.
Last edited by iseahound on 07 Oct 2021, 18:31, edited 1 time in total.
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: [a119] ImagePut - Puts an image from anywhere to anywhere (convert, crop, scale)

01 Aug 2020, 16:46

a119 compatible
Spoiler
  • ImagePutHIcon() has been added. Returns a handle to an icon.
  • handles to icons: hIcon are now accepted as an input.
  • Pointers to stream are now accepted as an input.
  • Pointers to RandomAccessStream are now accepted as an input.
  • The hidden window behind icons: desktop is now accepted as an input.
  • ImagePut() now works as expected. (Was throwing errors due to improper v1 -> v2 translation.)
  • All functions that require writing to the disk now implement exponential backoff (max 6 tries). Helps remote access.
  • Calling ImagePutFile() without a filepath returns the generated filename as expected.
  • ImageType() now catches if you typed in a filename but forgot the extension.
  • ImagePutWallpaper() will now set the wallpaper before deleting the file.
Last edited by iseahound on 07 Oct 2021, 18:30, edited 1 time in total.
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: [a122] ImagePut - Windows Image Transformation Library

23 Aug 2020, 10:41

a122 compatible (most likely, I can't test it because it's detected as a trojan on my workstation)
Spoiler
Last edited by iseahound on 07 Oct 2021, 18:30, edited 1 time in total.
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: [a122] ImagePut - Windows Image Transformation Library

15 Sep 2020, 10:20

a122 compatible
Spoiler
  • Added ImagePutWindow(). Displays an image in a window with transparency. Alpha values are completely transparent. Only works on Windows 8+.
  • ImagePutFile() properly interprets destination filepaths.
  • Fix warning when using a url as input.

Code: Select all

ImagePutWindow("https://i.imgur.com/YZk4Rfg.png")
Last edited by iseahound on 27 Jan 2024, 23:38, edited 2 times in total.
Dsul3791
Posts: 1
Joined: 12 Jan 2021, 12:43
Contact:

Re: [a122] ImagePut - Windows Image Transformation Library

12 Jan 2021, 12:53

Hello and thank you for this project. I am trying to use it for the first time. Im including the ImagePut.ahk library like such, at the top of my script.

#inlcude ImagePut.ahk

When I try to start my script I get this error from the library.

Error at line 120
Line Text: call(cotype, ByRef image, crop := "", scale := "", terms*) {
Error: Invalid class variable declaration.

Is this something new, old or have I gotten an old library?

Your help will be greatly appreciated
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

Re: [a122] ImagePut - Windows Image Transformation Library

14 Jan 2021, 11:01

There are two versions of the library, one for AutoHotkey version 1 and another for AutoHotkey version 2.

The error is because you are trying to run v2 code on AutoHotkey v1.

To fix this error, use the v1 library.
v1 releases: https://github.com/iseahound/ImagePut/releases
v1 forum : https://www.autohotkey.com/boards/viewtopic.php?f=6&t=76301

Warning - Direct Download: https://github.com/iseahound/ImagePut/releases/download/r2021.01.14/ImagePut.v1.zip
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

[a134] ImagePut - Library for moving image data on Windows

17 May 2021, 20:57

a134 compatible
Spoiler
Last edited by iseahound on 07 Oct 2021, 18:29, edited 1 time in total.
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

ImagePut v1.1

28 Sep 2021, 20:18

v2-beta.1 compatible
ImagePut v1.1
This release contains several important bugfixes.
  • Default file names generated by ImagePutFile are now unique.
  • URL validation RegEx has been updated be more selective.
  • Streams are no longer double freed which could potentially cause memory crashes.
  • [v2] ImagePutWindow now allows the script to exit after window closed.
  • [v2] A temporary file created by ImagePutWallpaper is now properly deleted after 2 seconds.
Last edited by iseahound on 04 Feb 2023, 10:30, edited 1 time in total.
iseahound
Posts: 1434
Joined: 13 Aug 2016, 21:04
Contact:

ImagePut - Library for moving image data on Windows

07 Oct 2021, 18:28

v2-beta.1 compatible
ImagePut v1.2
Support for streams as a intermediate has been added to base64, file, hex, RandomAccessStream, stream, and url. Previously, ImagePutFile(url) would convert via: url → decode → pixels → encode → file. Now it can directly convert: url → stream → file.
  • Increase speed by skipping encoding and decoding of compressed images when using streams.
  • File hashes are now preserved when moving between stream compatible data types.
  • File extensions are also preserved from the input where possible.
  • For previous behavior, the flag ForceDecodeImagePixels has been added.
  • An additional flag, ForcePushImageToMemory controls whether pBitmaps should load their pixel data immediately.
ImageEqual has been completely revamped.
  • Will now throw errors if supplied with an improper image.
  • Calling with one image parameter will now verify that image is valid. Previously, ImageEqual(image) always returned true.
  • Bitmaps are now cloned to solve the problem where multiple calls to LockBits would fail, if the bitmaps shared the same stream. Calling LockBits from the clone will lock the original bitmap so the underlying stream is never locked.
  • CloneImage has been replaced with GdipCloneBitmapAreaI to preserve the native PixelFormat when cloning.
  • Support for bitmaps with negative stride is implicit when cloning.
Bugfixes
  • Declared types {type:image} were broken by v1.1.
  • A 24-bit image passed to put_clipboard would cause the script to crash.
  • Some applications expected a bottom-up bitmap from the clipboard.
  • Not supplying a valid window handle to OpenClipboard would cause EmptyClipboard to crash.
  • Calling ObjRelease() on a clipboard stream would delete the clipboard data.
  • select_codec() now throws instead of defaulting to PNG.
Internal Documentation
  • from_XX and put_XX has been factored into get_XX and set_XX if the function used a stream.
  • There are two types of streams: pStream from CreateStreamOnHGlobal and pMemoryStream from SHCreateMemStream.
  • Calls to SHCreateMemStream (pMemoryStream) have been removed as GetHGlobalFromStream is only supported on pStreams.
  • All get_XX functions create pStreams. All set_XX functions read both pStream and pMemoryStream.
  • Multiple bitmaps created on a single stream are now resolved by calling either GdipImageForceValidation through the ForcePushImageToMemory flag, or by calling GdipCloneImage on a given pBitmap.
  • put_screenshot now uses bilinear scaling.
  • put_screenshot defaults to the top left corner of the screen.
  • Return values of uint have been replaced with HRESULT in v2.
Last edited by iseahound on 04 Feb 2023, 10:30, edited 1 time in total.

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: IfThenElse and 23 guests