What is a PGM File?

A Portable Graymap file is a Netpbm raster format in which each pixel represents a grayscale intensity. It uses a straightforward text or binary structure rather than a feature-rich image container.

How PGM Files work

PGM begins with a magic identifier, image dimensions, and a maximum sample value, followed by one intensity sample for every pixel. Its plain variant writes values as decimal text, while its raw variant uses a compact binary payload; both describe the same grayscale grid. The format is useful at boundaries where transparent parsing matters, such as test fixtures, computer-vision inputs, and inspection between processing stages.

Key facts

  1. P5 marks the raw (binary) variant and P2 the plain variant, which writes each sample as ASCII decimal. Header fields are separated by whitespace and may be interrupted by comments that run from # to the end of the line. In raw PGM, a single whitespace character after the maximum value separates the header from the raster. A raw file may hold several images back to back, though the specification notes that most tools read only the first; a plain file holds exactly one.
  2. The maximum value in the header, not a fixed bit depth, defines the sample range: it must be greater than 0 and less than 65536, with 0 as black and the maximum as white. Raw samples take one byte when the maximum is below 256 and otherwise two bytes with the most significant byte first. Assuming one byte per pixel or little-endian order corrupts valid files; libjpeg v6b, for example, wrote little-endian 16-bit PGM files against the specification, as discussed on the FFmpeg developer list. For the widest compatibility the specification recommends a maximum of 255, because raw files could not exceed it before April 2000.
  3. A PGM header does not say how sample values relate to light. The specification defines them as intensity encoded with the ITU-R BT.709 transfer function, names linear values and the sRGB curve as common variations, and acknowledges that many files use the layout for arbitrary integers. Whether a file holds display gray levels, linear sensor data, a depth map, or a mask therefore has to be agreed outside the file.
  4. PGM carries one channel and no alpha, color profile, or structured metadata. Netpbm represents transparency as a second PGM used as a mask, and its PAM format can hold gray and alpha together in one file. No media type is registered with IANA; image/x-portable-graymap is the conventional label.

When PGM Files matter

Use PGM as an intermediate format for grayscale computer-vision data or simple debugging. Convert it for distribution when file size, metadata, transparency, or broad application support matters.

How Transloadit helps with PGM Files

Transloadit’s image formats table, which covers its ImageMagick-based Robots such as /image/resize, lists PGM as a format those Robots can read and write.

The table records format-level read and write support only. It does not say whether output is raw or plain PGM or how maximum values above 255 are handled, so run a representative file through your Assembly before relying on higher-precision data.

Explore Transloadit’s file formats & compression capabilities

Sources

Turn media knowledge into a working pipeline

Connect uploads, processing, AI, storage, and delivery through one declarative API — with the encoding stack, scaling, and format churn handled for you.

Try Transloadit for free