[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [Parameter(Mandatory = $true)] [string]$OutDir, [ValidateRange(1, 100)] [int]$JpegQuality = 95, [switch]$Recurse, [switch]$Force ) $ErrorActionPreference = 'Stop' Add-Type -AssemblyName System.Drawing function Save-Jpeg { param( [System.Drawing.Image]$Image, [string]$Target, [int]$Quality ) $encoder = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where-Object { $_.MimeType -eq 'image/jpeg' } | Select-Object -First 1 $parameters = New-Object System.Drawing.Imaging.EncoderParameters(1) $parameter = New-Object System.Drawing.Imaging.EncoderParameter( [System.Drawing.Imaging.Encoder]::Quality, [long]$Quality ) $parameters.Param[0] = $parameter $Image.Save($Target, $encoder, $parameters) $parameters.Dispose() } $resolved = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).Path if (Test-Path -LiteralPath $resolved -PathType Leaf) { $root = Split-Path -Parent $resolved $files = @(Get-Item -LiteralPath $resolved) } elseif (Test-Path -LiteralPath $resolved -PathType Container) { $root = $resolved $files = @(Get-ChildItem -LiteralPath $resolved -File -Recurse:$Recurse -ErrorAction SilentlyContinue) } else { throw "Путь не найден: $resolved" } $allowed = @('.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff') $outRoot = [System.IO.Path]::GetFullPath($OutDir) if (-not (Test-Path -LiteralPath $outRoot)) { New-Item -ItemType Directory -Path $outRoot -Force | Out-Null } $done = 0 $skipped = 0 foreach ($file in $files) { $extension = $file.Extension.ToLowerInvariant() if ($extension -notin $allowed) { continue } $relative = $file.FullName.Substring($root.Length) -replace '^[\\/]+', '' $target = Join-Path $outRoot $relative if ((Test-Path -LiteralPath $target) -and -not $Force) { Write-Host "Пропуск: $target" $skipped++ continue } $parent = Split-Path -Parent $target if (-not (Test-Path -LiteralPath $parent)) { New-Item -ItemType Directory -Path $parent -Force | Out-Null } $image = [System.Drawing.Image]::FromFile($file.FullName) try { $orientation = 1 try { $orientation = [int]$image.GetPropertyItem(274).Value[0] } catch { $orientation = 1 } if ($orientation -eq 1) { Write-Host "Пропуск, поворот не нужен: $relative" $skipped++ continue } switch ($orientation) { 2 { $image.RotateFlip([System.Drawing.RotateFlipType]::RotateNoneFlipX) } 3 { $image.RotateFlip([System.Drawing.RotateFlipType]::Rotate180FlipNone) } 4 { $image.RotateFlip([System.Drawing.RotateFlipType]::RotateNoneFlipY) } 5 { $image.RotateFlip([System.Drawing.RotateFlipType]::Rotate90FlipX) } 6 { $image.RotateFlip([System.Drawing.RotateFlipType]::Rotate90FlipNone) } 7 { $image.RotateFlip([System.Drawing.RotateFlipType]::Rotate270FlipX) } 8 { $image.RotateFlip([System.Drawing.RotateFlipType]::Rotate270FlipNone) } } if ($image.PropertyIdList -contains 274) { $image.RemovePropertyItem(274) } switch ($extension) { '.jpg' { Save-Jpeg -Image $image -Target $target -Quality $JpegQuality } '.jpeg' { Save-Jpeg -Image $image -Target $target -Quality $JpegQuality } '.png' { $image.Save($target, [System.Drawing.Imaging.ImageFormat]::Png) } '.bmp' { $image.Save($target, [System.Drawing.Imaging.ImageFormat]::Bmp) } '.tif' { $image.Save($target, [System.Drawing.Imaging.ImageFormat]::Tiff) } '.tiff' { $image.Save($target, [System.Drawing.Imaging.ImageFormat]::Tiff) } } } finally { $image.Dispose() } Write-Host "Повернуто: $relative" $done++ } Write-Host "Итог: $done повернуто, $skipped пропущено"