python-3.xbitmappngpython-imaging-library8-bit

Pillow convert png to 8bit bitmap


i tried to convert 8bit PNGs to 8bit(256indexed palette) Bitmap image, but Pillow is keep vomiting crappy outcome.

this is what i tried.

image = Image.open(file)
image = image.convert('P')
pp = image.getpalette()
pp[0] = 255
pp[1] = 0
pp[2] = 255
image.putpalette(pp)

or

image = Image.open(file)
image = image.convert('P')
image.save(blabla.bmp)

and this is the outcome what i expected to see. this is an actual bitmap(done by Photoshop.) Photoshop and this is what Pillow did: Pillow what kind of joke is this ?! and it even got cropped out what should i do to convert it correctly?

Original Image:

enter image description here


Solution

  • You can do it like this:

    from PIL import Image
    
    # Open image
    image = Image.open('feather.png')
    
    # Quantize to 256 colours using fast octree method
    result = image.quantize(colors=256, method=2)
    

    enter image description here