Удаление прозрачности из изображений GIF через PowerShell 5 - PullRequest
0 голосов
/ 24 января 2019

Я пишу скрипт для массового удаления прозрачности из файлов .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
...