在 PowerShell 中处理图像文件可能不是最常见的任务,但在某些场景下非常有用。本文将介绍一些实用的图像处理技巧。
首先,让我们看看基本的图像操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| function Get-ImageInfo { param( [string]$ImagePath ) Add-Type -AssemblyName System.Drawing $image = [System.Drawing.Image]::FromFile($ImagePath) $info = [PSCustomObject]@{ FileName = Split-Path $ImagePath -Leaf Width = $image.Width Height = $image.Height PixelFormat = $image.PixelFormat Resolution = $image.HorizontalResolution FileSize = (Get-Item $ImagePath).Length } $image.Dispose() return $info }
|
图像格式转换:
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
| function Convert-ImageFormat { param( [string]$InputPath, [string]$OutputPath, [ValidateSet("jpg", "png", "bmp", "gif")] [string]$TargetFormat ) try { Add-Type -AssemblyName System.Drawing $image = [System.Drawing.Image]::FromFile($InputPath) switch ($TargetFormat) { "jpg" { $image.Save($OutputPath, [System.Drawing.Imaging.ImageFormat]::Jpeg) } "png" { $image.Save($OutputPath, [System.Drawing.Imaging.ImageFormat]::Png) } "bmp" { $image.Save($OutputPath, [System.Drawing.Imaging.ImageFormat]::Bmp) } "gif" { $image.Save($OutputPath, [System.Drawing.Imaging.ImageFormat]::Gif) } } $image.Dispose() Write-Host "图像转换完成:$OutputPath" } catch { Write-Host "转换失败:$_" } }
|
图像调整:
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
| function Resize-Image { param( [string]$InputPath, [string]$OutputPath, [int]$Width, [int]$Height ) try { Add-Type -AssemblyName System.Drawing $image = [System.Drawing.Image]::FromFile($InputPath) $newImage = New-Object System.Drawing.Bitmap($Width, $Height) $graphics = [System.Drawing.Graphics]::FromImage($newImage) $graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic $graphics.DrawImage($image, 0, 0, $Width, $Height) $newImage.Save($OutputPath) $graphics.Dispose() $newImage.Dispose() $image.Dispose() Write-Host "图像调整完成:$OutputPath" } catch { Write-Host "调整失败:$_" } }
|
图像效果处理:
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
| function Apply-ImageEffect { param( [string]$InputPath, [string]$OutputPath, [ValidateSet("grayscale", "sepia", "blur", "sharpen")] [string]$Effect ) try { Add-Type -AssemblyName System.Drawing $image = [System.Drawing.Image]::FromFile($InputPath) $bitmap = New-Object System.Drawing.Bitmap($image) switch ($Effect) { "grayscale" { for ($x = 0; $x -lt $bitmap.Width; $x++) { for ($y = 0; $y -lt $bitmap.Height; $y++) { $pixel = $bitmap.GetPixel($x, $y) $gray = [int](($pixel.R * 0.3) + ($pixel.G * 0.59) + ($pixel.B * 0.11)) $bitmap.SetPixel($x, $y, [System.Drawing.Color]::FromArgb($gray, $gray, $gray)) } } } "sepia" { for ($x = 0; $x -lt $bitmap.Width; $x++) { for ($y = 0; $y -lt $bitmap.Height; $y++) { $pixel = $bitmap.GetPixel($x, $y) $r = [int](($pixel.R * 0.393) + ($pixel.G * 0.769) + ($pixel.B * 0.189)) $g = [int](($pixel.R * 0.349) + ($pixel.G * 0.686) + ($pixel.B * 0.168)) $b = [int](($pixel.R * 0.272) + ($pixel.G * 0.534) + ($pixel.B * 0.131)) $bitmap.SetPixel($x, $y, [System.Drawing.Color]::FromArgb($r, $g, $b)) } } } } $bitmap.Save($OutputPath) $bitmap.Dispose() $image.Dispose() Write-Host "已应用效果:$Effect" } catch { Write-Host "效果处理失败:$_" } }
|
这些技巧将帮助您更有效地处理图像文件。记住,在处理图像时,始终要注意内存使用和资源释放。同时,建议在处理大型图像文件时使用流式处理方式,以提高性能。