ImageResizeAndArchive/Process-Images.ps1

72 lines
2.3 KiB
PowerShell
Raw Normal View History

# Script to find, process, and anaylze the results of processing.
# eventually make this folder select.
$ImagePathToProcess = "C:\Users\eeckert\Downloads\Test Photos\Unmodified"
$ArchivePathBase = "\\192.168.24.15\OriginalPhotos\TestPath"
$ImageOutputPath = "C:\Users\eeckert\Downloads\Test Photos\AfterImageMagick - Pass 1"
$depth = 15 # how many folders down do we go.
$Reduction_Percentage = '25%'
$Logging = @()
if (!($ImagePathToProcess)) {
# If this path doesn't exist, then error out. Erik's a fool and has been burned by this before.
} else {
# do stuff!
# get file list - image formats only
$imagefiles = get-childitem $imagePathToProcess -Recurse -Depth $depth -Include ('*.jpg','*.png','*.bmp')
foreach ($image in $imagefiles) {
$copyResult = $null
# Set Paths
$SourcePath = $image.FullName
$SourceSize = $image.Length
$ArchivePathDest = $SourcePath.Replace($ImagePathToProcess,"") # strip sourcepath
$ArchivePath = $ArchivePathBase + $ArchivePathDest
$ArchivePath_Parent = Split-Path -Parent $ArchivePath
New-Item -Path $ArchivePath_Parent -ItemType Directory -Force
$copyresult = Copy-Item $SourcePath $ArchivePath -Force
if (!($copyresult)) {
$copyresult_Dest = $null
$actualDest_file = $ImageOutputPath + $SourcePath.Replace($ImagePathToProcess,"") # strip sourcepath
$actualDest_Parent = Split-Path -Parent $actualDest_file
New-Item -Path $actualDest_Parent -ItemType Directory -Force
$copyresult_Dest = Copy-Item $SourcePath $actualDest_file -Force
if (!($copyresult_Dest)) {
# if no error Assemble the imagemagick command line
$command = "magick"
$commandArgs = "mogrify `"$actualDest_file`" -resize $Reduction_Percentage"
Start-Process $command -ArgumentList $commandArgs -wait
$NewImage = get-item -path $actualDest_file
$NewSize = $NewImage.Length
$Logging += [PSCustomObject]@{
FileName = $SourcePath
'Original Size' = $SourceSize
'New Size' = $NewSize
2024-11-19 14:53:34 -07:00
'Reduction' =100 - (($NewSize / $SourceSize) * 100)
}
}
}
}
}