pythonpython-imaging-librarypbm

Convert PNG to PBM P4 with Pillow


I have a PNG image.

I can convert it to PBM using Pillow:

from PIL import Image

im = Image.open("myfig.png")
im.save("myfig.pbm")

However it seems that encoding P6 is used as default (https://en.wikipedia.org/wiki/Netpbm_format)

I would like to have P4 encoding. How can I do that with Pillow?


Solution

  • If you want P4, you need a single bit, binary image:

    im = Image.open("myfig.png").convert('1')
    im.save("myfig.pbm")