开发者

png24 to png8 conversion

开发者 https://www.devze.com 2023-01-06 04:34 出处:网络
I want to convert the input 24 bit PNG image to 8 bit, I have tried using Imagemagick and Python PIL, neither works.

I want to convert the input 24 bit PNG image to 8 bit, I have tried using Imagemagick and Python PIL, neither works.

for instance:

at Imagemagick I try convert console command as such:

convert -depth 8 png24image.png png8image.png

And here is the way I tried with python:

import Image
def convert_8bit(sr开发者_开发问答c, dest):
    """
    convert_8bit: String, String -> void.
    Converts the input image file into 8bit depth.
    """
    im = Image.open(src)
    if not im.mode == "P":
        im2 = im.convert("P", rgb2xyz)
        im2.save(dest)

Imagemagick doesn't even touch the image while the python function reduces to 8bit but keeps the number of unique numbers 164instead of 256. Photoshop used to convert the image to 8bit with 256unique numbers when converted to png8 from a 24 bit png image.

Thanks

EDIT:

Output of 24->8 png conversion via Photoshop (which I need) alt text http://img695.imageshack.us/img695/934/psout.png

Converted via my Python function

alt text http://img638.imageshack.us/img638/6762/pythonout.png


It looks like the quantization method on the convert method isn't doing the right thing. Try this:

im2 = im.convert('RGB').convert('P', palette=Image.ADAPTIVE)

The extra conversion to RGB may be redundant. I got this hint from http://nadiana.com/pil-tips-converting-png-gif


I do it like this (using Imagick in PHP):

$colors = min(255, $im->getImageColors());
$im->quantizeImage($colors, Imagick::COLORSPACE_RGB, 0, false, false );
$im->setImageDepth(8 /* bits */);


ImageMagick and PIL have low quality PNG8 support.

For best quality/filesize ratio (and full alpha support as a bonus) use pngquant2:

pngquant *.png
0

精彩评论

暂无评论...
验证码 换一张
取 消