<div class="gmail_quote">Hi,<br>I'm using little attached program to convert my RGB and RGBA PNG's to png8. But with RGBA pngs it's limited because it uses only GIF-like 1-bit transparency. Seems that the alpha transparency in 8-bit (and 4-bit) PNG's is little known or supported. But the main browsers seem to support it including IE 6.0. One opensource utility which supports writing these is: <a href="http://www.libpng.org/pub/png/apps/pngquant.html" target="_blank">http://www.libpng.org/pub/png/apps/pngquant.html</a> .<br>
<br>It would be greate if PIL would also do that.<br><br>Have a nice day,<br>Tibor<br><br><br><br>import Image<br>import random<br>import optparse<br>import sys<br><br>def quantize_and_invert(alpha):<br> if alpha <= 64: # You may change the threshold value into something other. <br>
return 255<br> return 0<br><br>im = Image.open(sys.argv[1])<br><br>if im.mode=='RGBA':<br> print "Warning, converting alpha transparency to 1-bit transparency"<br> al = im.split()[3]<br>
al = Image.eval(al, quantize_and_invert)<br> colors=255<br>else:<br> al = None<br> colors=256<br>if im.mode!='RGB':<br> im = im.convert('RGB')<br>im=im.convert('P', palette=Image.ADAPTIVE, colors=colors)<br>
if al: im.paste(255,None,al)<br>im.save('out.png',optimize=1, transparency=255)<br><br><br><br>
</div><br>