What is PGM? Portable Graymap Format Explained

PGM (Portable Graymap) is the grayscale member of the Netpbm family, created by Jef Poskanzer, storing uncompressed gray-level images in ASCII or binary form.

Free online file converter tool. Works in Chrome Firefox Safari Edge Opera and other modern browsers on Windows macOS Linux Android and iOS. No software installation or sign-up required. All conversions run directly in your browser, so your files never leave your device. Free to use with no account needed.

Image

What is PGM? Portable Graymap Format Explained

The grayscale member of the Netpbm family, storing simple uncompressed gray-level images.

Last updated:

Year Created1988
CompressionRaster image (grayscale)
Primary UseGrayscale interchange

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

DeveloperJef Poskanzer (Netpbm)[1]
File Extension.pgm[1]
MIME Typeimage/x-portable-graymap[1]
Released1988[1]
CompressionNone[1]

PGM vs Other Image Formats

FeaturePGMPNGBMP
TypeRaster (grayscale)RasterRaster
Color depthGrayscale, up to 16-bit[1]Up to 16-bitUp to 32-bit
CompressionNone[1]LosslessNone or RLE
TransparencyNoYesLimited
EncodingASCII or binary[1]BinaryBinary
Best forGrayscale exchange[2]Web graphicsWindows 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

Simple grayscale storage | FileFormer

PGM cleanly represents gray-level images with a minimal, easy-to-parse structure.

Plain and binary variants | FileFormer

The P2 ASCII form is human-readable, while the P5 raw form is more compact for the same image.

Adjustable bit depth | FileFormer

The maximum gray value field supports both 8-bit and 16-bit precision per pixel.

Portable and well-supported | FileFormer

It is widely handled by image-processing toolkits and is easy to generate programmatically.

Disadvantages

Grayscale only | FileFormer

PGM cannot store color; color images require the related PPM format.

No compression | FileFormer

Uncompressed storage makes files larger than compressed grayscale formats such as PNG.

Minimal metadata | FileFormer

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 Free

Frequently 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.

References

  1. PGM Format Specification - Netpbm Official Documentation
  2. Netpbm - Wikipedia
  3. Netpbm formats - Just Solve the File Format Problem