Home / Alt manpages / netpbm(1)

  • netpbm(1)
  • User command
  • linux

Build Safe Image Pipelines with Netpbm on Linux

You will finish with a small, testable Netpbm workflow: inspect an image, create one from text, scale it, and convert it through a PNG round trip. The commands use Netpbm 11.5.2 from Debian package 2:11.05.02-1.1build1. Allow about fifteen minutes, plus time to install the package if it is not already present.

Netpbm is a collection of command-line programs and a C library, not one interactive editor. Its strength is composition: one program writes an image to standard output and the next program reads it from standard input. The examples below avoid overwriting an existing file and do not require sudo.

1. Check the installed tools

Start by confirming the package and the programs you will call. This is read-only:

$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
$ command -v pbmtext pamfile pamscale pnmtopng pngtopam
/usr/bin/pbmtext
/usr/bin/pamfile
/usr/bin/pamscale
/usr/bin/pnmtopng
/usr/bin/pngtopam

Use the common -version option on a Netpbm program when you need the linked library version:

$ pamscale -version
pamscale: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
...build details may follow...

Checkpoint: if a command is missing, stop here and install the distribution's Netpbm package. Do not replace a missing executable with a similarly named tool without checking its manual.

2. Understand the image names

PBM means black and white, PGM means greyscale, and PPM means full colour. PAM is the more general format and can represent those visual images as well as data with extra planes. The collective name PNM refers to PBM, PGM and PPM; it is not a fourth raster format.

The program prefix is a useful guide. A pbm, pgm, ppm or pam program usually expects that family. A pnm program reads PBM, PGM and PPM while retaining their differences. Programs using the PGM, PPM or PAM library interfaces can often accept simpler formats through implied conversion. That convenience can also change the result: scaling a PBM image with pamscale promotes it to PGM output.

Inspect rather than guessing:

$ printf 'P1\n3 2\n0 1 0\n1 0 1\n' | pamfile -all -
-:     Image 0:     PBM plain, 3 by 2

The -all option makes pamfile consume the complete input stream. That matters when the input is a pipe; without it, the program normally reads only the first header.

3. Create a harmless test image

pbmtext renders a line or lines into a PBM image. Send text on standard input and remove the default margins for a compact test:

$ printf 'Netpbm\n' | pbmtext -nomargins | pamfile -all -
-:     Image 0:     PBM raw, 40 by 15

This command changes no files. To save the result, redirect it to a new pathname that you have checked first:

$ test ! -e /tmp/netpbm-text.pbm && printf 'Netpbm\n' | pbmtext -nomargins > /tmp/netpbm-text.pbm
$ pamfile -all /tmp/netpbm-text.pbm
/tmp/netpbm-text.pbm:    Image 0:    PBM raw, 40 by 15

Checkpoint: keep temporary experiments under /tmp or a dedicated working directory. A redirection with > truncates an existing file before the program runs, so the test ! -e guard is deliberate. If you need to replace this temporary file, remove it only after checking the exact path, then rerun the command.

4. Scale through a pipeline

pamscale accepts an absolute width or height. If you specify only one dimension, it preserves the aspect ratio. Specifying both independently can distort an image. This example deliberately asks for both dimensions so the output is predictable:

$ printf 'P1\n3 2\n0 1 0\n1 0 1\n' | pamscale -xsize 6 -ysize 4 | pamfile -all -
pamscale: promoting from PBM to PGM
-:     Image 0:     PGM raw, 6 by 4  maxval 255

The informational promotion message goes to standard error. Add -quiet when a script needs a clean diagnostic stream, but do not use it while learning what a pipeline is doing. The common -plain option asks a program that emits PNM for ASCII output instead of raw binary output:

$ printf 'P1\n3 2\n0 1 0\n1 0 1\n' | pamscale -plain -xsize 6 -ysize 4 | head -5
P2
6 4
255
255 255 0 0 255 255
255 255 0 0 255 255

Do not assume -plain affects PAM output. The manpage says it applies to PNM-producing programs, while PAM has no plain and raw variants.

5. Convert without creating an intermediate file

Netpbm includes format bridges. Convert the generated PBM to PNG, then back to a Netpbm image and inspect the result:

$ printf 'P1\n3 2\n0 1 0\n1 0 1\n' | pnmtopng | pngtopam | pamfile -all -
-:     Image 0:     PBM raw, 3 by 2

Only the final program's description is printed. The PNG exists only in the pipe, so there is no intermediate file to clean up. For a real conversion, redirect the final output to a new destination and inspect it first with pamfile. Avoid editing an input in place: a failed conversion can leave a truncated output if both names refer to the same file.

6. Keep format and resource limits visible

Netpbm programs generally process one row at a time, but some operations need a window or the whole image. The in-memory representation is larger than the file representation, particularly for colour images. A pipe may also be copied to a temporary file when a program needs to reread it. For large images, check available disk space as well as RAM before starting a long pipeline.

Transparency is not a general property of PBM, PGM and PPM. Traditional Netpbm workflows use a separate PGM opacity mask. Some newer programs use PAM tuple types such as RGB_ALPHA or GRAYSCALE_ALPHA. If alpha matters, inspect the PAM header instead of assuming that a PNM conversion preserved it.

The installed package on this machine does not provide netpbm-config, although the overview manpage describes it as present in a standard installation. Treat that command as optional and verify it with command -v netpbm-config before using it in a script.

Done means

  • You confirmed the installed package and linked Netpbm version.
  • You can distinguish PBM, PGM, PPM, PAM and the collective PNM name.
  • You inspected a stream with pamfile -all before relying on it.
  • You created and scaled an image without overwriting an input.
  • You converted through PNG using a pipeline with no unnecessary intermediate file.
  • You know when promotion, transparency and memory use can change the result.