Converting PNG32 to PNG8 with PIL while preserving transparency

前端 未结 4 962
悲哀的现实
悲哀的现实 2020-12-16 04:48

I would like to convert a PNG32 image (with transparency) to PNG8 with Python Image Library. So far I have succeeded converting to PNG8 with a solid background.

Belo

4条回答
  •  Happy的楠姐
    2020-12-16 05:17

    This is an old question so perhaps older answers are tuned to older version of PIL?

    But for anyone coming to this with Pillow>=6.0.0 then the following answer is many magnitudes faster and simpler.

    im = Image.open('png32_or_png64_with_alpha.png')
    im = im.quantize()
    im.save('png8_with_alpha_channel_preserved.png')
    

提交回复
热议问题