Home / Alt manpages / ppm(5)

  • ppm(5)
  • File format
  • linux

Build and Check Portable PPM Images with Netpbm

This guide shows how to create a small PPM image for testing, inspect what Netpbm sees, and convert it to greyscale without losing track of the format. It uses the installed Netpbm package, version 11.5.2, on a Debian-family system. Allow about ten minutes. No elevated privileges are needed.

What you are handling

PPM is the colour member of the portable anymap family. It is deliberately simple and deliberately inefficient. A normal, or raw, PPM image starts with P6, followed by a width, height and maximum sample value. The raster then stores red, green and blue samples for each pixel, row by row.

There is also plain PPM, marked P3. Its samples are ASCII decimal numbers, separated by whitespace. Plain PPM is convenient for a hand-written test because it is readable, but raw PPM is normally smaller and faster. The file suffix is conventionally .ppm, although the format does not require a particular filename.

Checkpoint

The two formats are not interchangeable by changing the suffix. The magic number at the beginning of the file identifies which representation the reader must parse.

1. Create a harmless two-pixel test image

Use a temporary directory so the example cannot overwrite an existing image. This plain PPM contains a red pixel followed by a blue pixel. Its dimensions are two by one, and its samples range from zero to 255.

$ workdir=$(mktemp -d)
$ printf 'P3\n2 1\n255\n255 0 0 0 0 255\n' > "$workdir/test.ppm"
$ pnmfile "$workdir/test.ppm"
$ rm -rf "$workdir"

Expected output is similar to:

/tmp/tmp.example/test.ppm: PPM plain, 2 by 1  maxval 255

The temporary directory and its test image are disposable. If you need to inspect the file again, repeat the first two commands instead of guessing its contents. rm -rf is destructive, so use it only with the exact temporary path returned by mktemp.

2. Read the header before processing an image

For an existing file, ask Netpbm to identify the format rather than trusting its name:

$ pnmfile /path/to/input.ppm

Look for the representation, dimensions and maxval. For the test above, the important values are PPM plain, 2 by 1 and maxval 255. A raw file will be reported as PPM raw.

Each pixel has three samples in red, green, blue order. A maxval greater than zero and less than 65536 is allowed. Values below 256 use one byte per sample in raw PPM; values from 256 through 65535 use two bytes, with the most significant byte first. That choice affects both file size and whether an old reader can open the image.

Checkpoint

Do not assume that every .ppm contains one image. The format permits a sequence of images with no separator. Many older tools read only the first image, so a single-image file with maxval 255 is the safest interchange choice.

3. Convert a PPM copy to greyscale

Netpbm's ppmtopgm reads PPM and writes PGM. Keep the original and redirect the converted stream to a different file:

$ ppmtopgm /path/to/input.ppm > /path/to/output.pgm
$ pnmfile /path/to/output.pgm

For the two-pixel test, the verification result is similar to:

/path/to/output.pgm: PGM raw, 2 by 1  maxval 255

This command changes the representation and discards the separate red, green and blue channels in the output. It does not rewrite the input. If the output path already exists, the shell will truncate it before ppmtopgm runs, so choose a new path or make a backup first.

No sudo is required for ordinary files you can read and write. If you are processing a protected system path, stop and decide whether changing that file is actually intended. Elevated privileges do not make an invalid PPM valid.

4. Keep colour values honest

True PPM sample values follow the BT.709 colour model and its transfer function. Files using sRGB, or files whose samples are linear rather than gamma-adjusted, are common variations. They may pass a structural check while displaying with different brightness or colour.

Record the variation when PPM is an interchange format between tools. Netpbm's pnmgamma can convert between the documented PPM convention and the common sRGB or linear variants. Do not use a filename or a successful pnmfile result as evidence that the pixels use the colour interpretation you need.

For an automated pipeline, keep the conversion step explicit and verify the result afterwards:

$ pnmgamma -bt709tolinear /path/to/input.ppm > /path/to/linear.ppm
$ pnmfile /path/to/linear.ppm

Check the installed command's help before choosing a gamma option for a particular source variation. The format specification describes the colour distinction; it does not identify the colour space of an arbitrary file that merely happens to use PPM syntax.

5. Prefer the compatible subset when sharing files

Many small programs implement only the simplest PPM reader. For broad compatibility, write one raw image with P6, ordinary whitespace, dimensions on one line, and maxval 255. Keep the raster binary and do not append a second image.

Plain PPM has an additional layout rule: no line should exceed 70 characters. Comments beginning with # are permitted in the header in both forms. Whitespace includes spaces, tabs, carriage returns, line feeds, vertical tabs and form feeds. Header fields are ASCII decimal text; the raster is binary in raw PPM.

If an older consumer rejects a modern-looking file, first inspect the header with pnmfile. Then produce a compatibility copy rather than editing bytes by hand. Netpbm tools such as pnmtopnm and pamdepth are intended for these representation changes; verify each copy before replacing anything.

Common failure modes

  • Wrong magic number: P3 means plain PPM and P6 means raw PPM. Changing the suffix does not fix a mismatch.
  • Invalid maximum: zero and values above 65535 are outside the format. Netpbm rejects an input with an oversized maximum.
  • Unexpected colours: valid structure does not settle whether samples are BT.709, sRGB or linear. Confirm the producer's convention.
  • Truncated output: shell redirection opens the destination before the converter runs. Never redirect onto the source file.
  • Old reader stops early: a multi-image PPM can be legal but still be read as one image by older software. Use one image for compatibility.

Done means

  • pnmfile reports the expected PPM representation, dimensions and maximum sample value.
  • You know whether the file is plain P3 or raw P6.
  • Conversions write to a separate destination and leave the source intact.
  • Any sRGB or linear-sample variation is recorded and converted deliberately.
  • Shared files use one raw image with maxval 255 when compatibility matters.