Sign in to follow this  
Followers 0
Insomniac420

Crop .png files from folder

5 posts in this topic

#1 ·  Posted (edited)

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

#3 ·  Posted (edited)

#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)
EndSwitch
EndIf

; Crop the images
_GDIPlus_Startup()
; 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
EndIf

$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)
EndIf
_GDIPlus_ImageDispose($hNewImage) ; Don't need it anymore
Next

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

_GDIPlus_Shutdown()


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)
#cs
If $sString = "" Then
$sString &= $sAppend
Else
$sString &= $sDelim & $sAppend
EndIf
#ce
; Same as above, but with the Ternary operator
$sString &= (($sString = "") ? ("") : ($sDelim)) & $sAppend
Return 1
EndFunc ;==>_StringAppend

Edited by DatMCEyeBall
1 person likes this

"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

@tabhooked

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.

HotKeySet("{end}","_Off")
HotKeySet("{insert}","_On")

$count=1


While 1
Sleep(200)
WEnd


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


Func _Off()
send("{altup}{ctrlup}{SHIFTup}")
While 1
Sleep(200)
WEnd
EndFunc

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  
Followers 0