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.
image crop and save function
Re: image crop and save function
Here's an example of a crop function: http://www.autohotkey.com/board/topic/2 ... ntry455137
Also:(gdip is required)
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
Re: image crop and save function
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)?
Re: image crop and save function
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.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.
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
}
Re: image crop and save function
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
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
Re: image crop and save function
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.
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.
ste(phen|ve) kunkel
Re: image crop and save function
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:
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)
}
Re: image crop and save function
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.
ste(phen|ve) kunkel
Re: image crop and save function
I mentioned WIA in regard to your question, but my script uses gdip.
Re: image crop and save function
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
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
ste(phen|ve) kunkel
Re: image crop and save function
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...
(moments later) Meh. I renamed the file to "Gpid.ahk" and updated the "#Include."
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...
(moments later) Meh. I renamed the file to "Gpid.ahk" and updated the "#Include."
ste(phen|ve) kunkel
Re: image crop and save function
If it's in your Lib folder, you shouldn't need to #Include it.
https://autohotkey.com/docs/Functions.htm#lib
https://autohotkey.com/docs/Functions.htm#lib
Re: image crop and save function
Sir,
It is possible to auto crop image in power builder tool using WIA .
If possible then How we cam implement it.
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.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.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 }
Who is online
Users browsing this forum: bobstoner289, Google [Bot] and 294 guests