-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresize.ps1
42 lines (42 loc) · 1.93 KB
/
resize.ps1
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
Add-Type -AssemblyName 'PresentationCore'
$l = Get-Content $args[0]
$n = 0
$c = 0
foreach ($p in $l) {
$b = [io.path]::GetFileNameWithoutExtension($p)
$n++
try {
$s = [System.IO.FileStream]::new($p, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
$d = [System.Windows.Media.Imaging.JpegBitmapDecoder]::new($s, [System.Windows.Media.Imaging.BitmapCreateOptions]::None, [System.Windows.Media.Imaging.BitmapCacheOption]::OnLoad)
} catch {
$c++
Write-Host ("Traitement de " + [io.path]::GetFileName($p) + " -> ignoré (format non reconnu)")
continue
} finally {
try {$s.Close()} catch {}
}
Write-Host ("Traitement de " + [io.path]::GetFileName($p))
$i = $d.Frames[0]
$m = 150 / [math]::Max($i.PixelWidth, $i.PixelHeight)
$r = $i.Metadata.GetQuery("/app1/ifd/exif:{ushort=274}") -bor 0
$g = [System.Windows.Media.TransformGroup]::new()
$g.Children.Add([System.Windows.Media.ScaleTransform]::new($m, $m))
Switch ($r) {{$_ -in 3, 4} {$g.Children.Add([System.Windows.Media.RotateTransform]::new(180)); break} {$_ -in 5, 6} {$g.Children.Add([System.Windows.Media.RotateTransform]::new(90)); break} {$_ -in 7, 8} {$g.Children.Add([System.Windows.Media.RotateTransform]::new(270)); break}}
$t = [System.Windows.Media.Imaging.TransformedBitmap]::new($i, $g)
$f = [System.Windows.Media.Imaging.BitmapFrame]::Create($t)
$e = [System.Windows.Media.Imaging.JpegBitmapEncoder]::new()
if ($d.ColorContexts -ne $null) {$e.ColorContexts = $d.ColorContexts}
$e.QualityLevel = 85
$e.Frames.Add($f)
$s = [System.IO.FileStream]::new($args[1] + " - " + $b + ".jpg", [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write)
$e.Save($s)
$s.Close()
}
if ($c -eq 0) {
Write-Host ($n.ToString() + " image(s) traitée(s)")
exit 0
} else {
Write-Host ($n.ToString() + " image(s) traitée(s) dont")
Write-Host (" " + $c.ToString() + " image(s) non reconnue(s)")
exit 1
}