All about PGM files
simple format for grayscale images
PGM is the grayscale variant of the PBM family. While PBM supports only black and white, PGM allows for up to 65,535 shades between the two extremes. Each pixel is described by a single numerical value: 0 is black, and the maximum value is white. PGM stands for "Portable Gray Map" and was developed in late 1988 by Jef Poskanzer as an extension of PBM. For the history of the entire format family, see my article on PBM.
TLDR:
PGM is the grayscale format of the Netpbm family. It works like PBM, only with more than two “colors”—namely, grayscale. PGM is still used in image processing, in education, and as an intermediate format in pipelines. Anyone familiar with the PBM article knows the principle; PGM simply makes it grayer. The color variant is, after all, PPM.
The technical structure
A PGM image differs from PBM by exactly one additional line in the header: the maximum value (Maxval). This specifies how many gray levels the image uses. With PGM, it is also possible to draw an image on "graph paper." Instead of coloring in squares, NUMBERS representing brightness are used in each square.
| Component | Example | Meaning |
|---|---|---|
| Magic Number | P2 |
Plain PGM (ASCII variant) |
| Comment | # mein bild |
Optional, introduced by # introduced |
| Width Height | 24 7 |
Dimensions in pixels |
| Maxval | <15 |
Maximum gray value (1–65,535) |
| Raster data | 0 2 4 6 ... |
Gray values per pixel |
The classic FEEP example looks like this in PGM; each letter is assigned its own brightness value. (For better visualization with a monospace font, I chose a Maxval of 8)
P2 # feep.pgm 24 7 8 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 2 2 2 2 0 0 4 4 4 4 0 0 6 6 6 6 0 0 8 8 8 8 0 0 2 0 0 0 0 0 4 0 0 0 0 0 6 0 0 0 0 0 8 0 0 8 0 0 2 2 2 0 0 0 4 4 4 0 0 0 6 6 6 0 0 0 8 8 8 8 0 0 2 0 0 0 0 0 4 0 0 0 0 0 6 0 0 0 0 0 8 0 0 0 0 0 2 0 0 0 0 0 4 4 4 4 0 0 6 6 6 6 0 0 8 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
The text "FEEP" is recognizable, just like in the PBM example, but the letters become lighter from left to right: F is dark gray (2), E is medium gray (4 and 6), P is white (8).
Maxval: the key difference between PBM and PGM
Maxval determines the color depth of the image. Incidentally, with Maxval 1, PGM behaves exactly like PBM. With Maxval 255, you have 256 shades of gray, which corresponds to a classic 8-bit grayscale image. Values above 255 are possible (up to 65535), but are rarely used in practice.
Plain vs. Raw: P2 and P5
Just like with PBM, PGM also has an ASCII and a binary variant:
| Property | Plain PGM (P2) |
Raw PGM (P5) |
|---|---|---|
| Pixel data | ASCII decimal numbers | Binary, 1 or 2 bytes per pixel |
| Human-readable | Yes, completely | Header only |
| Storage space per pixel | Variable (digits + whitespace) | 1 byte (Maxval ≤ 255) or 2 bytes |
| Max. line length | 70 characters recommended | No limit |
The RAW variant (P5) is the standard for Netpbm tools. For a Maxval greater than 255, two bytes per pixel are used, big-endian.
Typical applications
PGM is used wherever the pure brightness value per pixel matters and color is irrelevant:
– Science and medicine: X-ray images, MRI scans, and microscopy images are often processed as grayscale images. PGM is suitable for this due to its simplicity and lossless storage.
– Computer vision: Edge detectors (Sobel, Canny), thresholding methods, and histogram analyses work with grayscale data. PGM provides this without overhead.
– Elevation maps and terrain models: Each pixel represents an elevation—an application that fits perfectly with PGM’s “one value per pixel” logic.
– Transparency masks: In Netpbm, PGM is also used as a transparency mask. Here, white represents full opacity and black represents complete transparency.
– Education: Like PBM, PGM is ideal for teaching the fundamentals of image processing (not image editing).
Gamma and Color Space
According to the specification, the gray values are not linear but follow the gamma transfer function per ITU-R BT.709 (gamma 2.2). In practice, however, many programs do not adhere to this and write linear values. The Netpbm tool pnmgamma can convert between the two variants. The now more common sRGB transfer function also deviates slightly from BT.709, and in reality, most programs simply use sRGB and label the result as PGM.
Sources
Netpbm PGM Format Specification
PBM article with history of the format family
.b0 { color: #ccc; }
.b2 {color: #222; }
.b4 {color:#444;}
.b6 {color:#666;}
.b8 { color: #888; }
const el = document.getElementById('pgm');
el.innerHTML = el.textContent.replace(/([0-9])/g, '<span class="b$1">$1</span>');
Convert, open and edit PGM files
Details about PGM files
- Software for opening PGM files
- IrfanView
- XnView
- GIMP
- ImageMagick
- Software for editing PGM files
- GIMP
- ImageMagick
- Paint.NET
- MIME-type for PGM
- image/x-portable-graymap
- image/x-pgm
Last updated on April 23, 2026 by
No Comments