image crop and save function

Get help with using AutoHotkey and its commands and hotkeys
sir crop-a-lot

image crop and save function

21 Apr 2015, 12:25

What is currently the best way to crop and save an existing image file without using external tools like irfanview?
I found this old forum post with code that contains crop and save functions. But it uses objects and I have a hard time extracting only the functions I need. And since that post is two years old maybe there is some newer better way to do this. All I want to input x y w h numbers and an image filepath and want to save to the same filepath. No gui needed.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: image crop and save function

21 Apr 2015, 14:13

Here's an example of a crop function: http://www.autohotkey.com/board/topic/2 ... ntry455137

Also:

Code: Select all

NewWidth := 300
NewHeight := 400
FileSelectFile, FilePath
SplitPath, FilePath,,, FileExt
if (FileExt = "bmp")
	Format := 0x21808
else
	Format := 0x26200A
pToken := Gdip_Startup()
pBitmapOld := Gdip_CreateBitmapFromFile(FilePath)
pBitmapNew := Gdip_CreateBitmap(NewWidth, NewHeight, Format)
G := Gdip_GraphicsFromImage(pBitmapNew)
Gdip_SetSmoothingMode(G, 4)
Gdip_SetInterpolationMode(G, 7)
Gdip_DrawImage(G, pBitmapOld, 0, 0, Gdip_GetImageWidth(pBitmapOld), Gdip_GetImageHeight(pBitmapOld))
Gdip_DisposeImage(pBitmapOld)
Gdip_SaveBitmapToFile(pBitmapNew, FilePath)
Gdip_DisposeImage(pBitmapNew)
Gdip_DeleteGraphics(G)
Gdip_Shutdown(pToken)
return
(gdip is required)
sir crop-a-lot

Re: image crop and save function

22 Apr 2015, 08:40

Thank you, that works. But is there also a way that doesn't require the gdip include? The code I linked to in the first post doesn't seem to use that (or am I missing something)?
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: image crop and save function

22 Apr 2015, 12:02

One way would be to copy the gdip functions and all of their dependent functions into your script.

I took a closer look at maestrith's code that you refer to in your original post. I haven't used the WIA.ImageFile object before, but this seems to be working. It borrows heavily from maestrith's script.

WARNING - this script overwrites the original image file.

Code: Select all

FileSelectFile, FilePath
if ErrorLevel
	return
crop(FilePath, 10, 20, 300, 400)

crop(FilePath, x, y, w, h) {
	static IP
	img := ComObjCreate("WIA.ImageFile")
	img.LoadFile(FilePath)
	if (!IP) {
		IP:=ComObjCreate("WIA.ImageProcess")
		ip.Filters.Add(IP.FilterInfos("Crop").FilterID)
	}
	
	; x,y,r,b are the number of pixels to crop from each edge. They cannot be negative numbers.
	ip.filters[1].properties("Left") := x
	ip.filters[1].properties("Top") := y
	if ((r := img.width - w - x) < 1)
		r := 0
	if ((b := img.height - h - y) < 1)
		b := 0
	ip.filters[1].properties("Right") := r
	ip.filters[1].properties("Bottom") := b
	img := IP.Apply(img)
	FileDelete, %FilePath%
	while FileExist(FilePath)
		Sleep, 10
	img.SaveFile(FilePath)
	return
}
Edit: You may want to add some error checks... like don't delete the original until after you verify that img.SaveFile worked. Then delete the original and rename/move the new file to the original's name/location.
sir crop-a-lot

Re: image crop and save function

22 Apr 2015, 14:54

Awesome! That shortens the code a lot. Thank you.

Some information I found once I knew what to look for

WIA.imagefile "Minimum supported client Windows Vista".
https://msdn.microsoft.com/en-us/librar ... 85%29.aspx
General information on WIA
https://msdn.microsoft.com/en-us/librar ... 85%29.aspx
kunkel321
Posts: 166
Joined: 30 Nov 2015, 21:19
Facebook: https://www.facebook.com/kunkel321
Google: kunkel321

Re: image crop and save function

17 Mar 2016, 15:06

Hi Kon and All,
I just found this thread while searching for scripts that crop... The one that Kon posted above (the one that starts with FileSelectFile ) works nicely, but I see that it does not allow negative numbers. Does anyone know of a similar setup that will allow negs? What I actually want, is to "shift" an image by removing 2 pixels from the top and the left, then adding 2 pixels to the bottom and right. (So the end result is the same size, just shifted up/left.) It's desktop wallpaper images that I'll be altering, so I already know the dimensions. They'll always be 1920x1080.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: image crop and save function

17 Mar 2016, 17:07

just me has since written a Windows Image Acquisition (WIA) lib, which simplifies using WIA in AHK. I'm not sure if it's capable of shifting the image the way you describe (although it could crop and stretch an image).

Using gdip:

Code: Select all

pToken := Gdip_Startup()
OnExit, CloseGdip
return

#c::
FileSelectFile, ImagePath, 3,,, Images (*.bmp; *.dib; *.rle; *.jpg; *.jpeg; *.jpe; *.jfif; *.gif; *.tif; *.tiff; *.png;)
if (Errorlevel)
    return
Shift2Px(ImagePath, A_ScriptDir "\MyNewImage.png", 0xAA123456)
return

CloseGdip:
Gdip_Shutdown(pToken)
ExitApp

Esc::ExitApp

; Note: If NewImagePath is blank, the original image will be overwritten 
Shift2Px(ImagePath, NewImagePath:="", Background:="") {
    SplitPath, ImagePath,,, FileExt
    Format := FileExt = "bmp" ? 0x21808 : 0x26200A ; 24 / 32 bit
    pBitmapOld := Gdip_CreateBitmapFromFile(ImagePath)
    Gdip_GetImageDimensions(pBitmapOld, Width, Height)
    pBitmapNew := Gdip_CreateBitmap(Width, Height, Format)
    pGraphics := Gdip_GraphicsFromImage(pBitmapNew)
    Gdip_SetSmoothingMode(pGraphics, 4)
    Gdip_SetInterpolationMode(pGraphics, 7)
    if (Background != "") {
        pBrush := Gdip_BrushCreateSolid(Background)
        Gdip_FillRectangle(pGraphics, pBrush, 0, 0, Width, Height)
        Gdip_DeleteBrush(pBrush)
    }
    Gdip_DrawImage(pGraphics, pBitmapOld, -2, -2, Width - 2, Height - 2)
    Gdip_DisposeImage(pBitmapOld)
    Gdip_SaveBitmapToFile(pBitmapNew, NewImagePath = "" ? ImagePath : NewImagePath)
    Gdip_DisposeImage(pBitmapNew)
    Gdip_DeleteGraphics(pGraphics)
}
kunkel321
Posts: 166
Joined: 30 Nov 2015, 21:19
Facebook: https://www.facebook.com/kunkel321
Google: kunkel321

Re: image crop and save function

18 Mar 2016, 15:26

Thanks Kon! With that last script, I'm getting an error because the WIA.ahk library here Windows Image Acquisition (WIA) lib doesn't seem to have the correct functions... I'll post a question on that thread about it though.
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: image crop and save function

18 Mar 2016, 15:27

I mentioned WIA in regard to your question, but my script uses gdip.
kunkel321
Posts: 166
Joined: 30 Nov 2015, 21:19
Facebook: https://www.facebook.com/kunkel321
Google: kunkel321

Re: image crop and save function

18 Mar 2016, 15:29

Thanks Kon! With that last script, I'm getting an error because the WIA.ahk library here Windows Image Acquisition (WIA) lib doesn't seem to have the correct functions... I'll post a question on that thread about it though.
EDIT: Wait a minute, these are WIAxyz functions, rather than GDIPxyz. Is there another library somewhere?
ED2 Found it! :) http://www.autohotkey.net/~tic/Gdip.ahk
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: image crop and save function

18 Mar 2016, 15:43

I currently recommend getting the gdip_all version (for Unicode/x64 compatibility), rename it to gdip.ahk, and put it in your Lib folder.
kon wrote:(gdip is required)
kunkel321
Posts: 166
Joined: 30 Nov 2015, 21:19
Facebook: https://www.facebook.com/kunkel321
Google: kunkel321

Re: image crop and save function

18 Mar 2016, 16:02

Thanks on the gdip_all tip!

So it looks like you've customized the script you posted yesterday just for me :) Thanks for that!
I have the script in a folder along with gpid_all.ahk. And the scrips has #Include, Gdip_All.ahk. I have a sample.png in the folder too. If I press Win+c, I can select my sample.png with the file selector dialog. But then nothing seems to happen. Does the parameter in here Shift2Px(ImagePath, A_ScriptDir "\MyNewImage.png", 0xAA123456) suggest that "MyNewImage.png" should be created in the same folder? It is not. Does A_ScriptDir need to have "%%" ?

Okay, wait.... Still trying to follow directions... :facepalm:
(moments later) Meh. I renamed the file to "Gpid.ahk" and updated the "#Include."
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: image crop and save function

19 Mar 2016, 23:33

If it's in your Lib folder, you shouldn't need to #Include it.
https://autohotkey.com/docs/Functions.htm#lib
rahul

Re: image crop and save function

03 May 2018, 00:35

Sir,
It is possible to auto crop image in power builder tool using WIA .
If possible then How we cam implement it.
kon wrote:One way would be to copy the gdip functions and all of their dependent functions into your script.

I took a closer look at maestrith's code that you refer to in your original post. I haven't used the WIA.ImageFile object before, but this seems to be working. It borrows heavily from maestrith's script.

WARNING - this script overwrites the original image file.

Code: Select all

FileSelectFile, FilePath
if ErrorLevel
	return
crop(FilePath, 10, 20, 300, 400)

crop(FilePath, x, y, w, h) {
	static IP
	img := ComObjCreate("WIA.ImageFile")
	img.LoadFile(FilePath)
	if (!IP) {
		IP:=ComObjCreate("WIA.ImageProcess")
		ip.Filters.Add(IP.FilterInfos("Crop").FilterID)
	}
	
	; x,y,r,b are the number of pixels to crop from each edge. They cannot be negative numbers.
	ip.filters[1].properties("Left") := x
	ip.filters[1].properties("Top") := y
	if ((r := img.width - w - x) < 1)
		r := 0
	if ((b := img.height - h - y) < 1)
		b := 0
	ip.filters[1].properties("Right") := r
	ip.filters[1].properties("Bottom") := b
	img := IP.Apply(img)
	FileDelete, %FilePath%
	while FileExist(FilePath)
		Sleep, 10
	img.SaveFile(FilePath)
	return
}
Edit: You may want to add some error checks... like don't delete the original until after you verify that img.SaveFile worked. Then delete the original and rename/move the new file to the original's name/location.

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, barracuda, Gio, Google [Bot], kczx3, Sanbenito, SeriousScrooge, TAC109, x32 and 168 guests