Home / Alt manpages / pgmtopgm(1)

  • pgmtopgm(1)
  • User command
  • linux

Turn PBM or PGM Input into a Reliable PGM Stream

You will finish with a repeatable way to feed a PBM or PGM image to pgmtopgm and save a PGM result that another Netpbm tool can read. The installed command is Netpbm 11.5.2, from Debian package netpbm 2:11.05.02-1.1build1.

Allow about ten minutes. You need a shell, a readable PBM or PGM file, and somewhere to write the result. The examples do not need elevated privileges. This command only transforms the stream; it does not edit the input file, install anything, or change system configuration.

1. Check the installed command

Confirm which executable the shell will run and record the local Netpbm version:

$ command -v pgmtopgm
/usr/bin/pgmtopgm
$ pgmtopgm --version 2>&1 | head -2
pgmtopgm: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
pgmtopgm: Built from source dated 2024-03-31 09:09:47

The manual describes no options specific to pgmtopgm. It reads standard input and writes standard output, so the input and output file names belong in shell redirections rather than after the command. The command also recognises common libnetpbm options, but do not add one unless you have a particular reason and have checked it on this installation.

2. Inspect the input before converting it

Use file to identify the candidate image without changing it:

$ file /path/to/input.pbm
/path/to/input.pbm: Netpbm image data, size 2 x 2, bitmap

Replace /path/to/input.pbm with your real path. A PBM is a black-and-white bitmap. A PGM is a greyscale image. Both are valid input for this command, because Netpbm programs can read PBM data in a PGM context.

Checkpoint: make sure the input is readable and leave the original in place:

$ test -r /path/to/input.pbm && echo readable
readable

If this prints nothing, fix the path or read permission first. Do not use sudo as a reflex. Running the conversion as root can leave a root-owned output that your normal account cannot replace.

3. Convert to a new PGM file

Write to a new name so that a failed run cannot replace a useful result:

$ pgmtopgm < /path/to/input.pbm > /path/to/output.pgm
$ status=$?
$ printf 'pgmtopgm exit status: %s\n' "$status"
pgmtopgm exit status: 0

Status 0 means the command completed successfully. The input remains untouched. The output is written as a binary PGM stream, whose magic number is P5. For example, a tiny PBM input can produce output like this:

$ od -An -c -N 15 /path/to/output.pgm
   P   5  \n   2       2  \n   2   5   5  \n

The header is text and the pixels after it are binary, so do not open the complete file in a text editor. The exact byte count shown by od depends on the image dimensions and header spacing.

4. Verify the result as an image

Check the output with file, then ask a Netpbm inspection tool for its dimensions if pnmfile is installed:

$ file /path/to/output.pgm
/path/to/output.pgm: Netpbm image data, size 2 x 2, rawbits, greymap
$ pnmfile /path/to/output.pgm
/path/to/output.pgm:	PGM raw, 2 by 2  maxval 255

Your wording may differ between versions. Look for a PGM image, the expected width and height, and a non-zero file size. The conversion does not resize the image or improve its quality. For PBM input, black and white pixels become the corresponding greyscale values in the PGM output. For PGM input, the image content is preserved while the stream is emitted in the standard raw PGM form.

Checkpoint: compare the dimensions with the source. If the result has unexpected dimensions, stop before passing it to a later image-processing step and inspect the source with the same tool.

5. Handle an existing destination safely

Shell redirection with > truncates an existing destination before pgmtopgm starts. That is easy to miss in a script. Use a temporary name in the same directory, verify it, then replace the destination deliberately:

$ tmp='/path/to/output.pgm.new'
$ pgmtopgm < /path/to/input.pbm > "$tmp" && file "$tmp"
/path/to/output.pgm.new: Netpbm image data, size 2 x 2, rawbits, greymap
$ mv -- "$tmp" /path/to/output.pgm

The mv command is the state-changing step: it replaces the old output if one exists. Do not run it until the temporary file passes your checks. If conversion fails, leave the old output alone and remove only the incomplete temporary file with rm -- /path/to/output.pgm.new. That removal is irreversible, so confirm the path before running it. If you need a rollback after a successful replacement, keep a backup first:

$ cp --preserve=all /path/to/output.pgm /path/to/output.pgm.bak

6. Diagnose the common failures

A message such as bad magic number means the input does not begin with a recognised Netpbm image marker, or that the stream is not an image at all. Check the path and inspect the first bytes without modifying the file:

$ file /path/to/input.pbm
$ od -An -c -N 16 /path/to/input.pbm

Valid PBM and PGM files normally begin with P1, P4, P2 or P5. A PPM file begins with a different marker and is not the input type described by this command. If you need to convert a known PBM to PGM with more control over the conversion, the Netpbm manual points to pbmtopgm as the more general tool.

A non-zero exit status is a failed conversion. Do not treat a partially written output as valid merely because the file exists. Check the status immediately after the command, and use a temporary destination when a previous output matters.

Done means

  • The installed Netpbm version and executable were checked.
  • The input is a readable PBM or PGM file and remains untouched.
  • pgmtopgm received its image through standard input and returned status 0.
  • The result is a verified raw PGM file with the expected dimensions.
  • An existing output was protected until the replacement had passed its checks.