up vote 4 down vote favorite
3
share [g+] share [fb]

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

I'm trying to convert an existing PNG32 image with alpha channel to PNG8. I have tried the following methods:

  • convert original.png PNG8:new.png - Horribly distorts the image and preserving only binary alpha (Not Indexed alpha)
  • GIMP - Fails as well, but produces better quality (good color quantizer) than ImageMagick.
  • pngcrush -rem alla -reduce -brute original.png new.png - Made the image smaller but didn't take quantization into account (Image has less than 256 colors), so output was still PNG32.

What else can I try?

link|improve this question
feedback

3 Answers

up vote 1 down vote accepted
+250

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
link|improve this answer
feedback

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!

link|improve this answer
pngquant doesn't preserve alpha channel. It produces the same output as GIMP. – LiraNuna Oct 7 '09 at 20:23
pngout will not cooperate - Unable to compress further: copying original file – LiraNuna Oct 7 '09 at 20:36
wow. can you make this image available somewhere? I'd like to take a crack at it. – DaveParillo Oct 8 '09 at 0:47
Just take any PNG32 image, I'm actually using one of the icons from Silk iconset - famfamfam.com/lab/icons/silk. – LiraNuna Oct 9 '09 at 4:15
All the icons in famfamfam_silk_icons_v013.zip appear to be 8-bit. I'm having trouble finding a 32 bit image on the web. Don't want to make one myself using the tools that are causing your grief as that's not likely a valid test. – DaveParillo Oct 9 '09 at 4:38
show 1 more comment
feedback

I know I'm a bit late to the party, but this recently bit me on a web project I'm working on.

I used pngnq for batch conversion and my PNG8 have never looked better. In fact, with the highest sampling rate (-s 1) the results are nearly indistinguishable from a full PNG32.

link|improve this answer
What a useful link, thanks! I've been looking for such a tool for some time. Tested pngnq and it produces an excellent result, small file and smooth alpha. – jjrv Nov 26 '11 at 21:21
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.