Home » Questions » Computers [ Ask a new question ]

Converting PNG32 file to PNG8 without losing alpha channel

Converting PNG32 file to PNG8 without losing alpha channel

I'm using Linux and have no access to any of Adobe's 'fancy' programs.

Asked by: Guest | Views: 296
Total answers/comments: 2
Guest [Entry]

"This PHP script does the trick with libgd:

<?PHP

if(!isset($argv[1]) || !is_readable($argv[1])) {
echo ""Creates an 8-bit PNG from a 32-bit PNG\n\n"";
echo ""Usage:\n"";
echo ""\t"" . $argv[0] . "" input.png > output.png\n"";
echo ""\t"" . $argv[0] . "" input.png output.png\n"";
die();
}

$inFile = $argv[1];
$outFile = $argv[2] or STDOUT;

$inImage = imagecreatefrompng($inFile);
$outImage = imagecreate(imagesx($inImage), imagesy($inImage));

imagecopy($outImage, $inImage, 0, 0, 0, 0, imagesx($inImage), imagesy($inImage));

imagepng($outImage, $outFile);

Dump that into a file and run it as:

php convert.php input.png output.png"
Guest [Entry]

"Your distro may include pngquant. If you can't find it with yum / apt-get, go to the web site. I think this is your best bet.

If you're having issues with pngquant, you can try pngout, but it's a long shot. I've heard it does a good job retaining the alpha channel, but can be slow. Possibly the slowness people experience is because the default 'strategy' is 'Extreme' which the author admits is slow.

It's a windows program, but there are linux ports (supposedly), but the link on the pngout authors page is out of date. The linux port maintainer, JonoF, maintains a page here now.

I have to admit to not ever having used it. Good luck!"