Pillow convert png to 8bit bitmap

放肆的年华 提交于 2019-12-24 19:19:11

问题


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:


回答1:


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)



来源:https://stackoverflow.com/questions/56800095/pillow-convert-png-to-8bit-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!