The Netpbm Family: PBM, PGM, PPM and Magic Numbers P1–P6
Why are there five whole formats when they all do roughly the same thing? Where do the cryptic codes P1, P5, P6 at the start of a file come from? And what does "text" versus "binary" actually mean? This is a short but dense tour of the most logical format family in history — no filler, concrete examples throughout.
One family, one idea
Netpbm is not one format but a whole family of close relatives. They all grew from a single idea that programmer Jef Poskanzer had in the 1980s: an image should be described so simply that it can be mailed as plain text and read effortlessly by any program. If you're new to these formats, start with "What Is PPM" — here we go deeper.
The brilliance of the family is that every format follows one template: magic number → dimensions → (max value) → pixel data. Learn one and you automatically understand the rest. Only one thing changes: how much information each pixel carries.
Why different formats
A black-and-white picture doesn't need three color channels — that would just waste space. So the family offers one format per case: bit, gray, color. Nothing redundant.
The trio: PBM, PGM, PPM
At the family's core are three formats, differing only in how much color they store per pixel:
- PBM (Portable Bitmap) — the most austere. Each pixel is 1 bit: black or white, no in-between. Ideal for barcodes, faxes, black-and-white masks.
- PGM (Portable Graymap) — shades of gray. One brightness channel per pixel (usually 0–255). This is a "photo without color."
- PPM (Portable Pixmap) — full color. Three channels per pixel: red, green, blue. This is the one you'll most often meet in practice.
Notice the pattern in the names? Bitmap → Graymap → Pixmap. Bit, Gray, Pixel — exactly in order of increasing data.
Magic numbers P1–P6
Now the fun part. How does a program know which format it's looking at? By the first two characters of the file — the magic number. That's the letter P plus a digit. There are six digits, distributed with perfect logic:
| Magic no. | Format | Stores | Form |
|---|---|---|---|
| P1 | PBM | Black & white (1 bit) | Text (ASCII) |
| P2 | PGM | Grayscale | Text (ASCII) |
| P3 | PPM | Color (RGB) | Text (ASCII) |
| P4 | PBM | Black & white (1 bit) | Binary (raw) |
| P5 | PGM | Grayscale | Binary (raw) |
| P6 | PPM | Color (RGB) | Binary (raw) |
The logic is rock-solid: odd numbers (1, 2, 3) are the text variant, even ones (4, 5, 6) are binary. And within each pair, color complexity grows: bit → gray → RGB. Memorize this table and you can decode any Netpbm file just by glancing at its first two characters.
ASCII vs binary: one format, two faces
The most common confusion: how does "text" PPM differ from "binary" PPM if the picture is the same? The difference is in how those same numbers are written. Look at one tiny color pixel (R=255, G=0, B=0 — pure red).
In the text variant (P3) it looks like this — readable by eye:
P3
1 1
255
255 0 0
In the binary one (P6) the header stays text, but the pixels themselves are raw bytes: 255, 0, 0 sit as three consecutive bytes, with no spaces or line breaks. In Notepad it would just look like random symbols, but the file is several times smaller and a program reads it instantly.
About maxval and 16 bits
The maxval in the header sets the maximum channel brightness. If it's below 256, each color uses 1 byte; if higher (up to 65535), it uses 2 bytes instead of 1, which lets PPM store much finer color detail — handy for scientific work.
PNM and PAM: umbrella and extension
Documentation often mentions two more names — and they cause confusion, though it's all simple.
PNM (Portable Anymap) is not a separate format but an umbrella name for PBM, PGM and PPM. Family tools accept "any of the three" and work out from the magic number what they've actually been handed. The .pnm extension just means "this is some Netpbm thing, sort it out on the spot."
PAM (Portable Arbitrary Map), magic number P7, is a later and more advanced extension. Its header is more flexible, and unlike the others it can store any kind of color data — including an alpha channel (transparency), which classic PBM/PGM/PPM lack.
Which variant to choose
In practice the choice is simple. For real work and storage you pick the binary variants (P4/P5/P6) — they're several times smaller. The text ones (P1/P2/P3) shine for learning, debugging and when you want to open a file by hand and see what's inside.
Either way, a PPM file won't display in a browser and most editors won't take it. So the final step is almost always the same — convert it to PNG or JPG. Exactly how is laid out in "How to Open PPM", and a size-and-compression comparison of PPM against PNG and BMP is in "PPM vs PNG and BMP".
Turn any Netpbm file into a normal image
P3 or P6, it doesn't matter — the FormatZ converter reads the file and hands you a compact PNG in a couple of seconds, right in your browser.
Convert PPM to PNG

