I'm writing a script to bulk remove transparency from .gif files. The result must still be in .gif format.
Tried conversion to .jpeg and back, but the transparent layer was not removed. Script shown below. Also tried color depth, but did not work. How to do so?
Reformatted the script to take up less space in the post.
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