Deep Dive

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.

Colorful code showing data types on screen — Netpbm magic numbers declare the file type
The magic number at the start of a file is the data type of the whole image. Photo: Pexels

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.

A close-up of source code — the format structure is simple and readable
From bit to color: the same file skeleton, only the amount of data per pixel changes. Photo: Pexels

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.FormatStoresForm
P1PBMBlack & white (1 bit)Text (ASCII)
P2PGMGrayscaleText (ASCII)
P3PPMColor (RGB)Text (ASCII)
P4PBMBlack & white (1 bit)Binary (raw)
P5PGMGrayscaleBinary (raw)
P6PPMColor (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.

Text (P3): size
Large
Text (P3): readability
By eye
Binary (P6): size
Several times smaller
Binary (P6): readability
Program only
Bytes per pixel (P6)
3 (maxval < 256)
Header
Always text

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.

A project file tree and code — choosing between the text and binary variant
Text or binary — the same data, just written two different ways. Photo: Pexels

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.

6magic numbers (P1–P6)
3base formats
P7is PAM with alpha

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
Netpbm isn't five different formats. It's one very logical format with five modes for every occasion.
It is the first two characters of the file — the letter P and a digit from 1 to 6. From them a program instantly knows what format it is looking at: P1 is a black-and-white PBM in text form, P6 is a color PPM in binary. The magic number is the passport of a Netpbm file.
They differ in how much color they store per pixel. PBM stores only two states — black or white (1 bit). PGM stores shades of gray (a single brightness channel). PPM stores full color — three channels R, G and B. The structure is the same for all: a header plus pixel data.
In the text variant (the odd numbers P1, P2, P3) pixels are written as ordinary numbers you can read by eye. In the binary one (the even P4, P5, P6) the same data is stored as raw bytes. The binary variant is several times more compact and faster for a program to read; the text one is clearer for a human.
PNM (Portable Anymap) is not a separate format but an umbrella name for PBM, PGM and PPM: a tool figures out the type from the magic number. PAM (Portable Arbitrary Map, magic number P7) is a newer extension with an arbitrary number of channels and support for transparency (an alpha channel).
For real work you almost always pick the binary variants (P4/P5/P6) — they are more compact. The text ones (P1/P2/P3) are handy for learning, debugging and when you want to peek inside the file by hand. And to get a normal viewable image, you convert any of them to PNG or JPG.