What is PGM? Portable Graymap Format Explained
The grayscale member of the Netpbm family, storing simple uncompressed gray-level images.
Last updated:
What is PGM?
PGM, the Portable Graymap format, is the grayscale member of the Netpbm family of image formats developed by Jef Poskanzer. It extends the simple Netpbm design to store gray-level images where each pixel holds a single intensity value.
A PGM file begins with a magic number, P2 for the plain ASCII variant or P5 for the binary (raw) variant, followed by the width, height, and a maximum gray value, then the pixel data. The maximum value defines the white point and allows either 8-bit or 16-bit gray levels, with no compression applied.
How PGM Works
A PGM header carries four whitespace-separated tokens: the magic number, the image width, the height, and a maximum gray value (maxval).[1] Each pixel is a single intensity sample from 0 (black) up to maxval (white). When maxval is 255 or less, raw samples occupy one byte; up to 65535 they occupy two bytes in most-significant-byte-first order, allowing 8-bit or 16-bit grayscale with no compression.[1]
Plain and Raw Variants
The P2 magic number marks the plain ASCII encoding, in which samples are written as decimal text, while P5 marks the raw binary encoding that stores samples as bytes.[1] Raw files are far more compact and are the common choice; plain files remain human-readable and email-safe.[3]
Place in the Netpbm Family
PGM sits between PBM and PPM in the Netpbm hierarchy created by Jef Poskanzer, adding gray levels to PBM's single bit while remaining far simpler than PPM's color triples.[2] Because the format is uncompressed and well documented, it is widely used as a lossless interchange and intermediate format in computer-vision and scientific-imaging workflows.[3]
MKV Technical Specifications
PGM vs Other Image Formats
| Feature | PGM | PNG | BMP |
|---|---|---|---|
| Type | Raster (grayscale) | Raster | Raster |
| Color depth | Grayscale, up to 16-bit[1] | Up to 16-bit | Up to 32-bit |
| Compression | None[1] | Lossless | None or RLE |
| Transparency | No | Yes | Limited |
| Encoding | ASCII or binary[1] | Binary | Binary |
| Best for | Grayscale exchange[2] | Web graphics | Windows images |
PGM offers a minimal, uncompressed way to store grayscale images, favoring simplicity over the compression and color features of PNG or BMP.
Advantages & Disadvantages
Advantages
PGM cleanly represents gray-level images with a minimal, easy-to-parse structure.
The P2 ASCII form is human-readable, while the P5 raw form is more compact for the same image.
The maximum gray value field supports both 8-bit and 16-bit precision per pixel.
It is widely handled by image-processing toolkits and is easy to generate programmatically.
Disadvantages
PGM cannot store color; color images require the related PPM format.
Uncompressed storage makes files larger than compressed grayscale formats such as PNG.
The format carries only essential image dimensions and a max value, with no rich metadata support.
Common Use Cases
PGM is a convenient intermediate format for grayscale image processing.
Computer vision input | FileFormer
Many image-processing and vision tools accept PGM grayscale images for analysis.
Netpbm conversions | FileFormer
PGM serves as an intermediate when converting grayscale images between formats.
Scientific and depth data | FileFormer
Single-channel intensity or height data can be stored straightforwardly as PGM.
Convert PGM Files Free
Use our free online image converter to convert PGM and other formats - no signup, no watermarks.
Try Image Converter FreeFrequently Asked Questions
What does PGM stand for?
PGM stands for Portable Graymap, the grayscale member of the Netpbm family of image formats.
What are the magic numbers P2 and P5?
P2 identifies a plain ASCII PGM file and P5 identifies the binary (raw) PGM variant; both store grayscale images.
What bit depths does PGM support?
The maximum gray value in the header determines precision, supporting up to 8-bit or 16-bit gray levels per pixel.
How is PGM different from PBM?
PBM stores 1-bit black-and-white pixels, while PGM stores multi-level grayscale values, giving smooth shades rather than just black and white.
Is PGM compressed?
No. Like the rest of the core Netpbm formats, PGM stores pixel data uncompressed.