Convert PNG to PBM P4 with Pillow

384 Views Asked by At

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?

1

There are 1 best solutions below

0
Mark Setchell On BEST ANSWER

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

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