<#
===========================================================================================
| |
| Variables |
| |
===========================================================================================
#>
$inputDirectoryPath = "$PSScriptRoot"
$fileNameSearchPattern = "*.*"
$imageExts = @("avif", "bmp", "jp2", "jpg", "png", "tga", "tif", "webp")
$recurse = $false
$irfanviewFullPath = "C:\Program Files\IrfanView\i_view64.exe"
# Cropping values
$x = 12
$y = 20
$width = 620
$height = 560
# For cropping images that are jpg/jpeg
$jpgQuality = 95
<#
===========================================================================================
| |
| Functions |
| |
===========================================================================================
#>
function Show-WelcomeScreen {
Clear-Host
Write-Host ""
Write-Host " $($host.ui.RawUI.WindowTitle)"
Write-Host " +=========================================+"
Write-Host " | |"
Write-Host " | This script will search for image files |"
Write-Host " | in the current working directory, and |"
Write-Host " | use IrfanView to crop them using the |"
Write-Host " | specified cropping settings. |"
Write-Host " | |"
Write-Host " +=========================================+"
Write-Host ""
Write-Host " Script Settings " -ForegroundColor DarkGray
Write-Host " ========================" -ForegroundColor DarkGray
Write-Host " Input Directory Path....: $inputDirectoryPath" -ForegroundColor DarkGray
Write-Host " Recursive File Search...: $recurse" -ForegroundColor DarkGray
Write-Host " Search File Name Pattern: $fileNameSearchPattern" -ForegroundColor DarkGray
Write-Host " Search File Extensions..: $($imageExts -join ', ')" -ForegroundColor DarkGray
Write-Host " IrfanView Full Path.....: $irfanviewFullPath" -ForegroundColor DarkGray
Write-Host " JPG Quality.............: $jpgQuality%" -ForegroundColor DarkGray
Write-Host " Cropping.values.........: X=$x, Y=$y, Width=$width, Height=$height" -ForegroundColor DarkGray
Write-Host ""
}
function Confirm-Continue {
Write-Host " Press 'Y' key to continue or 'N' to exit."
Write-Host ""
Write-Host " -Continue? (Y/N)"
do {
$key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$char = $key.Character.ToString().ToUpper()
if ($char -ne "Y" -and $char -ne "N") {
[console]::beep(1500, 500)
}
} while ($char -ne "Y" -and $char -ne "N")
if ($char -eq "N") {Exit(1)}
}
function Validate-Parameters {
if (-not (Test-Path -LiteralPath $inputDirectoryPath -PathType Container)) {
Write-Host " Input directory path does not exists!" -BackgroundColor Black -ForegroundColor Red
Write-Host ""
Write-Host " Press any key to exit..."
$key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
Exit(1)
}
if (-not (Test-Path -LiteralPath $irfanviewFullPath -PathType Leaf)) {
Write-Host " Irfanview file path does not exists!" -BackgroundColor Black -ForegroundColor Red
Write-Host ""
Write-Host " Press any key to exit..."
$key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
Exit(1)
}
if ($imageExts.Count -eq 0) {
Write-Host " No image file extensions were specified!" -BackgroundColor Black -ForegroundColor Red
Write-Host ""
Write-Host " Press any key to exit..."
$key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
Exit(1)
}
}
function Crop-Files {
Clear-Host
$imageFiles = $null
if ($recurse) {
$imageFiles = Get-ChildItem -LiteralPath $inputDirectoryPath -Filter $fileNameSearchPattern -Recurse | Where-Object { $imageExts -contains $_.Extension.ToLower().TrimStart('.') }
} else {
$imageFiles = Get-ChildItem -LiteralPath $inputDirectoryPath -Filter $fileNameSearchPattern | Where-Object { $imageExts -contains $_.Extension.ToLower().TrimStart('.') }
}
foreach ($imageFile in $imageFiles) {
Write-Host " Cropping file: $($imageFile.FullName.Replace($inputDirectoryPath, "."))"
$exitCode = $null
try {
$process = New-Object System.Diagnostics.Process
$process.StartInfo.FileName = $irfanviewFullPath
$process.StartInfo.Arguments = "`"$($imageFile.FullName)`" /crop=($x,$y,$width,$height) /jpgq=$jpgQuality /convert=""$($imageFile.FullName)"""
$process.StartInfo.UseShellExecute = $false
$started = $process.Start() | Out-Null
$exited = $process.WaitForExit()
$exitCode = $process.ExitCode
} catch {
Write-Host ""
Write-Host " Error running IrfanView process." -BackgroundColor Black -ForegroundColor Red
Write-Host ""
Write-Error -Message ($_.Exception.Message)
Write-Host ""
Write-Host " Press any key to exit..."
$key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
Exit(1)
}
if ($exitCode -eq 0) {
Write-Host " File cropped successfully." -ForegroundColor DarkGreen
Write-Host ""
} else {
Write-Host " Error cropping file. IrfanView Exit Code: $exitCode" -BackgroundColor Black -ForegroundColor Red
Write-Host ""
Write-Host " Press any key to ignore and continue..."
$key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
}
}
}
function Show-GoodbyeScreen {
Write-Host " Operation Completed!" -BackgroundColor Black -ForegroundColor Green
Write-Host ""
Write-Host " Press any key to exit..."
$key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
Exit(0)
}
<#
===========================================================================================
| |
| Main |
| |
===========================================================================================
#>
[System.Console]::Title = "Crop image files - by Elektro"
#[System.Console]::SetWindowSize(146, 27)
[CultureInfo]::CurrentUICulture = "en-US"
try { Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Scope "Process" } catch { }
Show-WelcomeScreen
Validate-Parameters
Confirm-Continue
Crop-Files
Show-GoodbyeScreen