I didn't know how to use the code posted earlier in the thread. So, I asked AI to make one where I could execute it on Windows Powershell. It's not elegant and it converts it pretty slowly. Make a backup folder first though if you want to keep the old pictures.
$Folder = 'Copy Paste Your Folder Path Between These Two Apostrophes'
Add-Type -AssemblyName System.Drawing
Get-ChildItem -Path $Folder -Filter *.png -File | ForEach -Object {
$file = $_ .FullName
try {
$bytes = [System.IO.File]::ReadAllBytes($file )
$ms = New -Object System.IO.MemoryStream (,$bytes )
$origImg = [System.Drawing.Image]::FromStream($ms )
$ms .Dispose()
} catch {
Write-Warning "Cannot open $file "
return
}
$width = $origImg .Width
$height = $origImg .Height
$pixelFormat = [System.Drawing.Imaging.PixelFormat]::Format32bppArgb
$rect = [System.Drawing.Rectangle]::new(0 ,0 ,$width ,$height )
try {
$bmp = New -Object System.Drawing.Bitmap $width , $height , $pixelFormat
$g = [System.Drawing.Graphics]::FromImage($bmp )
$g .CompositingMode = [System.Drawing.Drawing2D.CompositingMode]::SourceCopy
$g .DrawImage($origImg , 0 , 0 , $width , $height )
$g .Dispose()
} catch {
$origImg .Dispose()
Write-Warning "Failed to create drawable bitmap for $file "
return
}
try {
$bmpData = $bmp .LockBits($rect ,
[System.Drawing.Imaging.ImageLockMode]::ReadWrite,
$pixelFormat )
} catch {
$bmp .Dispose()
$origImg .Dispose()
Write-Warning "LockBits failed for $file " [Expand Post] return
}
try {
$stride = $bmpData .Stride
$absStride = [Math]::Abs($stride )
$totalBytes = $absStride * $height
$buffer = New -Object byte[] $totalBytes
[System.Runtime.InteropServices.Marshal]::Copy($bmpData .Scan0, $buffer , 0 , $totalBytes )
$needsChange = $false
for ($i = 0 ; $i -lt $buffer .Length; $i += 4 ) {
if ($buffer [$i + 3 ] -lt 255 ) { $needsChange = $true ; break }
}
if (-not $needsChange ) {
[System.Runtime.InteropServices.Marshal]::Copy($buffer , 0 , $bmpData .Scan0, 0 )
$bmp .UnlockBits($bmpData )
$bmp .Dispose()
$origImg .Dispose()
Write-Host "No transparency: $file "
return
}
for ($i = 0 ; $i -lt $buffer .Length; $i += 4 ) { $buffer [$i + 3 ] = 255 }
[System.Runtime.InteropServices.Marshal]::Copy($buffer , 0 , $bmpData .Scan0, $totalBytes )
$bmp .UnlockBits($bmpData )
} catch {
try { $bmp .UnlockBits($bmpData ) } catch {}
$bmp .Dispose(); $origImg .Dispose()
Write-Warning "Error processing pixel buffer for $file "
return
}
$tmp = "$file .tmp.png"
try {
$pngEnc = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() |
Where-Object { $_ .MimeType -eq 'image/png' }
$bmp .Save($tmp , $pngEnc , $null )
} catch {
$bmp .Dispose(); $origImg .Dispose()
Write-Warning "Failed to save temp image: $tmp "
return
}
$bmp .Dispose()
$origImg .Dispose()
try {
Remove-Item -LiteralPath $file -Force
Rename-Item -LiteralPath $tmp -NewName ([System.IO.Path]::GetFileName($file ))
Write-Host "Transparency removed (RGB preserved): $file "
} catch {
Write-Warning "Failed to replace original file: $file "
if (Test-Path $tmp ) { Remove-Item -LiteralPath $tmp -Force }
}
}