What is a PBM File?
A Portable Bitmap file is a Netpbm raster format for monochrome images. Each pixel has one of two values, represented through a deliberately simple text or binary encoding.
How PBM Files work
PBM is the bilevel member of the Netpbm family, next to PGM (grayscale) and PPM. PNM is a collective name for those three, and PAM is a fourth, more general format. A PBM image is a short ASCII header followed by a raster: a two-character magic number, the width and the height as decimal numbers separated by whitespace, and then the rows from top to bottom, each running left to right. PBM has no maximum-value field, which PGM adds after the height. Before the raster, text from a # to the end of the line is a comment. The raw form packs pixels into bytes, while the older plain form writes each pixel as an ASCII 1 or 0. Richer images need PGM, PPM, PAM, or another format.
Key facts
- 1Identify the variant from the first two bytes, not the suffix:
P4is the packed binary (raw) form andP1is the ASCII (plain) form. The conventional suffix is.pbm, but.pnmis also used for PBM, PGM, and PPM alike, and no media type is registered with IANA (image/x-portable-bitmapis the conventional value). Check that your library reads both variants: Pillow’s documentation says it writes the raw forms and reads plainP1toP3files only from version 9.2.0. - 2In raw PBM each row is packed 8 pixels to a byte, most significant bit first, and the last byte of the row is filled with don’t-care bits. A row therefore takes the width divided by 8, rounded up, in bytes, and a reader that treats the raster as one continuous bit stream misaligns every row after the first whenever the width is not a multiple of 8. Exactly one whitespace character separates the height from the raster.
- 3A stored
1is black and a0is white, the reverse of PGM, where zero is black and the maximum value is white, and of a PAM image with tuple typeBLACKANDWHITE, which uses 0 for black and 1 for white. Code that maps the stored PBM bits directly to lightness values or PAM samples has to invert them. A decoding library may already have done so: Pillow turns a stored 1 into 0 and a stored 0 into 255, so check what your library returns before inverting again. - 4The two variants differ in more than encoding. A plain file holds exactly one image, has no fill bits, ignores whitespace inside the raster, and should keep lines to 70 characters. A raw file may hold several images back to back, which the specification has allowed since July 2000, and it notes that most tools still read only the first. For the widest compatibility the specification recommends writing
P4, a newline, the width and height separated by a space, a newline, and a single raster.
When PBM Files matter
Use PBM as an intermediate format for bilevel data, such as binary masks passed between image tools. The Netpbm specification calls it an intermediary that pays no heed to efficiency and says it is not a format one would normally use to store or transmit a file. It cannot represent grayscale, color, or partial transparency without conversion to another format.
How Transloadit helps with PBM Files
Transloadit’s image formats table lists PBM as a format that its ImageMagick-based Robots, including /image/resize, can read and write, and that Robot’s format parameter accepts any format the table marks as writable.
The table does not say whether PBM output is written in the raw or the plain variant, so check the magic number of the output if the next tool needs one of them.
Explore Transloadit’s file formats & compression capabilities
Sources
- Netpbm: PBM format specification (layout, minimal subset, plain PBM, media type, compatibility)
- Netpbm: PNM, the collective name for PBM, PGM, and PPM
- Netpbm: PGM format specification (maximum value, zero is black)
- Netpbm: PAM format specification (BLACKANDWHITE tuple type, opacity planes)
- Pillow handbook: PPM section (PBM, PGM, PPM read and write support by version)
- Pillow source: PpmImagePlugin.py (PBM decoding with inverted bits)
- Pillow issue #860: cannot identify image file when opening ASCII .pbm, .pgm, and .ppm files