Jump to content
Sign in to follow this  

Crop .png files from folder

Recommended Posts


Hi, I have a folder with 500+ .png files inside it, i need them to be cropped but not resizes i was looking in the examples forum and could only find examples for resizing and not cropping. Please if you guys can direct me to the right func or help me with a example i would appreciate it much ty. Also i'm using 64 bit i found a couple examples that where old and only in 32bit.

Edited by Insomniac420

Share this post

Link to post
Share on other sites

You could try Irfanview, has a command line interface you can easily write a script around

Share this post

Link to post
Share on other sites

#include <File.au3>
#include <GDIPlus.au3>
#include <MsgBoxConstants.au3>
Global Const $sInFolder = "In\" ; The folder where all your original .pngs are
Global Const $sOutFolder = "Out\" ; The folder where the new .pngs will be stored

; Generate a list of all the png files in $sInFolder
Global $asList = _FileListToArray($sInFolder, "*.png", 1, False)
If @error Then
Switch @error
Case 1
_ErrMsgExit("Folder not found or invalid, " & $sInFolder, -11)
Case 2
_ErrMsgExit("Invalid _FileListToArray() filter supplied", -12)
Case 3
_ErrMsgExit("Invalid _FileListToArray() Flag", -13)
Case 4
_ErrMsgExit("No files found in " & $sInFolder, -14)

; Crop the images
; I'm assuming that all the input files are different sizes and that the top left (0, 0)
; needs to be cropped into a 500 x 400 frame (NOTE: if the file is too small (under 500 x 400) then a blank space will occur)
Global $hImage, $hNewImage, $hCtxt, $sErrorList = ""
For $i = 1 To $asList[0] ; Loop through all files
$hImage = _GDIPlus_ImageLoadFromFile($asList[$i]) ; Attempt to load file
If @error Then
_StringAppend($sErrorList, "Error loading file: """ & $asList[$i] & """ @extended = " & @extended)
ContinueLoop ; Start the loop again since we can't use this image

$hNewImage = _GDIPlus_BitmapCreateFromScan0(500, 400) ; Creata a blank 500 x 400 bitmap
$hCtxt = _GDIPlus_ImageGetGraphicsContext($hNewImage) ; Get the GraphicsContext of an image (so we can write to it)
; _GDIPlus_GraphicsClear($hCtxt, 0xFF000000) ; Uncomment if you want the background to be a solid colour
_GDIPlus_GraphicsDrawImageRectRect( _
$hCtxt, $hImage, _
0, 0, 500, 400, _ ; Source image ($hImage) (X, Y, Width, Height) (ie. Take data from $hImage at XY 0, 0 with a width of 500 and height of 400)
0, 0, 500, 400, _ ; Destination image ($hCtxt -> $hNewImage) (ie. Write to $hNewImage at XY 0, 0 with a width of 500 and height of 400)
) ; Note: Keep the source image and destination image Width / Height the same to prevent resizing

; At this point $hNewImage contains the new data

_GDIPlus_GraphicsDispose($hCtxt) ; We no longer need to write to it, so get rid of it
_GDIPlus_ImageDispose($hImage) ; We no longer need to read from it, so get rid of it
_GDIPlus_ImageSaveToFile($hNewImage, $sOutFolder & $asList[$i]) ; Write $hNewImage to the folder
If @error Then
_StringAppend($sErrorList, "Error saving file: """ & $sOutFolder & $asList[$i] & """ @extended = " & @extended)
_GDIPlus_ImageDispose($hNewImage) ; Don't need it anymore

If $sErrorList <> "" Then
MsgBox($MB_OK + $MB_ICONWARNING, "Warning", "The following errors have occured:" & @CRLF & @CRLF & $sErrorList)


Func _ErrMsgExit($sMsg, $iCode, $iLine = @ScriptLineNumber)
If Not @Compiled Then $sMsg = "An error occured on line " & $iLine & ":" & @CRLF & @CRLF & $sMsg
MsgBox($MB_OK + $MB_ICONERROR, "Critical error", $sMsg)
Exit $iCode
EndFunc ;==>_ErrMsgExit

Func _StringAppend(ByRef $sString, $sAppend, $sDelim = @CRLF)
If $sString = "" Then
$sString &= $sAppend
$sString &= $sDelim & $sAppend
; Same as above, but with the Ternary operator
$sString &= (($sString = "") ? ("") : ($sDelim)) & $sAppend
Return 1
EndFunc ;==>_StringAppend

Edited by DatMCEyeBall
  • Like 1

"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha


Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Share this post

Link to post
Share on other sites

Well I know there are smarter and faster ways to do this but it worked. So I downloaded Ifranview I noticed it had auto crop so I just made a simple automated macro type func  to crop 493 .png files it did the job in a few mins slow but worked.



While 1

Func _On()
    ShellExecute (@ScriptDir&"\"&$count&".png") ;opend the .png file
    send("{SHIFTDOWN}{ctrldown}y{SHIFTup}{ctrlup}") ; auto crop the file
    send("{ctrldown}s{ctrlup}") ; save image
    send("{altdown}s{altup}") ; pressing save
    send("{altdown}y{altup}") ; pressing yes
    while ProcessExists("i_view32.exe")
    send("{esc}") ; closing Irfanviewr
    Until $count>493

Func _Off()
While 1

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this