PBM(5)

๐Ÿ’– pbm โ€” the Netpbm bi-level image format, bestie edition ๐Ÿ’–

okay so we're talking about the PBM format, and it lives inside the Netpbm(1) family (that's the manual page Netpbm(1), go read it, it's the mothership ๐Ÿ›ธ).

PBM is the lowest common denominator monochrome file format, and honestly? that's the whole point. It's the common language that a whole huge family of bitmap image conversion filters speaks to each other. It does NOT care about efficiency โ€” not even a little โ€” and that's exactly why it's so simple and so general that you can whip up a program to convert to or from basically any other graphics format, or mess with the image directly. iconic behavior honestly.

The name "PBM" is an acronym that comes from "Portable Bit Map." ๐Ÿ’…

fr fr, this is not a format you'd normally use to store a file or send it to somebody. It's too expensive and not expressive enough for that. It's just an intermediary format. In its purest use, it lives only in a pipe between two other programs. that's its main character moment ๐ŸŽ€

โœจ THE LAYOUT โœจ

Here's the actual format definition, bestie.

A PBM file is made of a sequence of one or more PBM images. And the way that there is no data, no delimiters, and no padding before, after, or between images. zero. none. they just vibe next to each other. ๐Ÿฆ‹

Each PBM image is built from this:

  • A "magic number" so the file knows what it is. A PBM image's magic number is the two characters "P4".
  • Whitespace (blanks, TABs, CRs, LFs).
  • The width in pixels of the image, formatted as ASCII characters in decimal.
  • Whitespace.
  • The height in pixels of the image, again in ASCII decimal.
  • A single whitespace character (usually a newline).
  • A raster of Height rows, in order from top to bottom. Each row is Width bits, packed 8 to a byte, with don't care bits to fill out the last byte in the row. Each bit represents a pixel: 1 is black, 0 is white. The order of the pixels is left to right. The order of their storage within each file byte is most significant bit to least significant bit. The order of the file bytes is from the beginning of the file toward the end of the file.

A row of an image is horizontal. A column is vertical. The pixels in the image are square and contiguous. ๐Ÿ“

  • Before the whitespace character that delimits the raster, any characters from a "#" to but not including the next carriage return or newline character, or end of file, is a comment and is ignored.

(Before June 2024, the carriage return or line feed was specified to be part of the comment and ignored, but in the 22 years that comments had existed in the specification, Netpbm never implemented that. so. yeah. ๐Ÿ’€)

All characters referred to here are encoded in ASCII. "newline" means the character ASCII calls Line Feed, aka LF. A "white space" character is space, CR, LF, TAB, VT, or FF โ€” i.e. exactly what the ANSI standard C isspace() function calls white space.

๐ŸŒธ Minimal Subset ๐ŸŒธ

The PBM format is so simple that a ton of programmers write programs that process it directly, without ever touching a library like libnetpbm, reverse engineering a subset of the format from examples instead of reading the actual spec. (we've all been there ๐Ÿซถ)

So if you want to produce an image that as many programs as possible can read, bestie, you should stick to this subset:

The image consists of the following bytes:

  • 'P4' followed by newline
  • width and height separated by a space (e.g. '50 100'), followed by newline
  • The raster

There is only one image in the file.

The program pnmtopnm produces this minimal subset, so if you have a more complex PBM image, you can pass it to a less tolerant program through pnmtopnm. slay โœจ

๐ŸŽ€ Plain PBM ๐ŸŽ€

Okay so plot twist: there is actually another version of the PBM format. It's even more simplistic, and even more lavishly wasteful of space than PBM. It's called Plain PBM.

Plain PBM actually came first, but even its own inventor couldn't stand its recklessly squanderous use of resources after a while, and switched to what we now know as the regular PBM format. ๐Ÿ˜ญ

But here's the thing โ€” Plain PBM is so redundant, so overstated, that it's virtually impossible to break. You can send it through the most liberal mail system (which was literally the original purpose of the PBM format) and it will arrive still readable. You can flip a dozen random bits and easily piece the original image back together. And honestly we hardly even need to define the format here, because you can decode it by inspection. it's giving "so simple it's self-documenting" ๐Ÿ‘‘

Netpbm programs generate Raw PBM format instead of Plain PBM by default, but the common option -plain chooses Plain PBM.

The difference is:

  • There is exactly one image in a file.
  • The "magic number" is "P1" instead of "P4".
  • Each pixel in the raster is represented by a byte containing ASCII '1' or '0', representing black and white respectively. There are no fill bits at the end of a row.
  • White space in the raster section is ignored.
  • You can put any junk you want after the raster, if it starts with a white space character.
  • No line should be longer than 70 characters.

Here is an example of a small image in the plain PBM format.

P1
# feep.pbm
24 7
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0
0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0
0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1 0
0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0
0 1 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

There is a newline character at the end of each of these lines.

You can generate the Plain PBM format from the regular PBM format (first image in the file only) with the pnmtoplainpnm program.

Programs that read this format should be as lenient as possible, accepting anything that looks remotely like a bitmap. be cool about it ๐Ÿ˜Œ


๐ŸŒˆ INTERNET MEDIA TYPE ๐ŸŒˆ

No Internet Media Type (aka MIME type, content type) for PBM has been registered with IANA, but the value image/x-portable-bitmap is conventional.

Also note that the PNM Internet Media Type image/x-portable-anymap applies too. two for one, we love that ๐Ÿ“


๐Ÿ’œ FILE NAME ๐Ÿ’œ

There are no requirements on the name of a PBM file, but the convention is to use the suffix .pbm. pnm is also conventional, for cases where distinguishing between the particular subformats of PNM is not convenient. iykyk ๐Ÿ’…


โญ COMPATIBILITY โญ

Before July 2000, there could be at most one image in a PBM file. As a result, most tools that process PBM files ignore (and don't read) any data after the first image. so if you're stacking images in there, heads up โ€” a lot of tools are just gonna stop at the first one. ๐Ÿ™ˆ


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

libnetpbm(1), pnm(1), pgm(1), ppm(1), pam(1), programs that process PBM(1)


๐Ÿงš DOCUMENT SOURCE ๐Ÿงš

This manual page was generated by the Netpbm tool makeman from HTML source. The master documentation is at

http://netpbm.sourceforge.net/doc/pbm.html

okay bestie that's the whole page, you're officially a PBM expert now, no cap ๐Ÿซถ this was the netpbm documentation, dated 07 November 2025 โ€” the The PBM Format(5) page, from the File Formats Manual section, mwah ๐Ÿ’– go read the other pages, they're all right there, slay โœจ๐ŸŽ€๐Ÿ’…

๐Ÿ“ฆ where this page comes from