我正在编写一个脚本以批量删除.gif文件的透明度。结果必须仍然是.gif格式。
尝试转换为.jpeg并返回,但未删除透明层。脚本如下所示。还尝试了色深,但是没有用。 怎么做?
重新格式化了脚本,以减少帖子中的空间。
function ConvertImage{
Param (
[string]$path,
[string]$inputFormat,
[string]$outputFormat,
[bool]$newName
)
if (Test-Path $path) {
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
$codec = [Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() |
Where-Object { $_.FormatDescription -eq "$outputFormat" }
$ep = New-Object Drawing.Imaging.EncoderParameters
$ep.Param[0] = New-Object Drawing.Imaging.EncoderParameter ([System.Drawing.Imaging.Encoder]::Quality, [long]100)
foreach ($file in (ls "$path\*.$inputFormat")) {
$convertfile = New-Object System.Drawing.Bitmap($file.Fullname)
if ($newName) {
[string]$newName = $file.DirectoryName + "\New-" + $file.Name
$newfilname = ($newName -replace "([^.]).$inputFormat",'$1') + ".$outputFormat"
} else {
$newfilname = ($file.FullName -replace "([^.]).$inputFormat",'$1') + ".$outputFormat"
}
$convertfile.Save($newfilname, $codec, $ep)
$newfilname #showing which file is processed
Start-Sleep -Milliseconds 200 #slowing down a bit
}
} else {
Write-Host "path not found."
}
}
ConvertImage -path "C:\Images" -inputFormat "gif" -outputFormat "jpeg" -newName $true
Read-Host "First conversion complete. Continue"
ConvertImage -path "C:\Images" -inputFormat "jpeg" -outputFormat "gif" -newName $false