-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathJPegCompressRecursively.ps1
More file actions
52 lines (40 loc) · 3.63 KB
/
Copy pathJPegCompressRecursively.ps1
File metadata and controls
52 lines (40 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function Save-Jpeg (
[string]$imagePath,
[string]$imageOutPut,
$quality = $(if ($quality -lt 0 -or $quality -gt 100){throw( "quality must be between 0 and 100.")})
){
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$bmp = New-Object System.Drawing.Bitmap $imagePath
#Encoder parameter for image quality
$myEncoder = [System.Drawing.Imaging.Encoder]::Quality
$encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1)
$encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter($myEncoder, $quality)
# get codec
$myImageCodecInfo = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders()|where {$_.MimeType -eq 'image/jpeg'}
#save to file
$bmp.Save($imageOutPut,$myImageCodecInfo, $($encoderParams))
}
function CompressJpegFolder{
Param(
[ValidateScript({Test-Path $_ -PathType 'Container'})]
[string]
$infolderPath,
[ValidateScript({Test-Path $_ -PathType 'Container'})]
[string]
$outfolderPath,
[ValidateScript({$_ -gt 0 -or $_ -lt 100})]
$quality
)
$allChild = Get-ChildItem $infolderPath -R | Where-Object {($_.Extension -eq ".JPG") -or ($_.Extension -eq ".jpg")}
$totalCount = $allChild.Count
Write-Host $totalCount "jpeg files found..."
$count = 0
foreach($file in $allChild)
{
$count = $count+1
Write-Host $count "file processed on" $totalCount
$outFile = Join-Path -Path $outfolderPath -ChildPath ($file.Directory.BaseName + $file.BaseName + ".jpg")
Save-Jpeg $file.FullName $outFile $quality
}
}
#CompressJpegFolder "C:\Users\benoit\Documents\PhotoCompression\tfv 2010" "C:\Users\benoit\Documents\ldjs" 75