LinuxCommandLibrary

pbmtopgm

Convert PBM image to PGM image

TLDR

Convert PBM image to PGM by averaging the wxh-sized area surrounding each pixel

$ pbmtopgm [w] [h] [path/to/image.pbm] > [path/to/output.pgm]
copy

SYNOPSIS

pbmtopgm [-maxval=n] [pbmfile]

PARAMETERS

-maxval=n
    Sets PGM max gray value to n (1-65535). Default: 255.
Lower values create darker images.

DESCRIPTION

pbmtopgm is a utility from the Netpbm graphics toolkit that reads a Portable BitMap (PBM) image and produces a Portable GrayMap (PGM) image.

PBM is a simple black-and-white format where pixels are either 0 (black) or 1 (white). PGM supports up to 65535 gray levels, allowing richer tonal range. The conversion maps PBM black pixels to 0 and white pixels to the chosen maxval (default 255), creating a grayscale equivalent without interpolation or dithering.

This tool is ideal for workflows needing grayscale input from binary images, such as preprocessing for convolution filters (pnmconvol), scaling (pnmscale), or edge detection. It adheres to Unix conventions: reads from stdin or a file, writes PGM to stdout, enabling piping like pbmtopgm < input.pbm | pgmtoy4m | mpeg2enc.

Performance is excellent for large images due to minimal processing. The output preserves dimensions and aspect ratio exactly. Part of the mature Netpbm suite, it's cross-platform and dependency-free for basic use.

CAVEATS

Input must be valid PBM (P1 or P4); invalid files cause errors. No support for color or multi-page images. Output is always PGM (P2/P5), not binary if maxval > 1.

STANDARDS

Follows Netpbm extensions to PNM format specs. Compatible with PBM level 1 (ASCII) and 4 (binary).

EXAMPLES

pbmtopgm input.pbm > output.pgm
pbmtopgm -maxval=31 < input.pbm | pnmtopng > out.png

HISTORY

Developed by Jef Poskanzer in 1988 as part of original PBMPLUS package. Evolved into Netpbm (1990s+), with ongoing maintenance by SourceForge community. Widely used in early Unix/Linux imaging.

SEE ALSO

pgmtopbm(1), pbm(5), pgm(5), pnm(5), netpbm(1)

Copied to clipboard