Home / Alt manpages / pbmtopgm(1)

  • pbmtopgm(1)
  • User command
  • linux

Anti-alias PBM Line Art with pbmtopgm

You will turn a black-and-white PBM image into a greyscale PGM whose pixels represent the average of a rectangular neighbourhood. That softens hard edges in line art and gives later image processing more than two possible levels. The examples use Netpbm 11.5.2, installed here as package version 2:11.05.02-1.1build1.

Allow about ten minutes. You need the netpbm package, a readable PBM input and a writable working directory. The commands below are ordinary user commands. Nothing needs sudo, and the input image is only read.

1. Check the installed command

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

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

The manual page describes the command as pbmtopgm width height [pbmfile]. There are no pbmtopgm-specific options. The optional file argument can be omitted to read standard input, which is useful in a pipeline.

Checkpoint

If command -v prints nothing, install Netpbm through your normal package-management process before continuing. Do not substitute an unrelated image converter and assume that its averaging rules are the same.

2. Understand what the dimensions mean

The first two arguments are the width and height of the averaging rectangle, in input pixels. They are not resize dimensions. For every output position, pbmtopgm averages the surrounding black and white PBM pixels and writes a grey level to the corresponding PGM pixel.

Use odd values when possible, such as 3 3 or 5 5. An odd rectangle has a clear centre pixel, so the result is easier to reason about around a sharp line. A one-pixel rectangle does not provide useful smoothing: the manual notes that a PGM program can already read PBM as the minimum and maximum grey levels. Choose a rectangle larger than one pixel when anti-aliasing is the actual goal.

The input dimensions still determine the output dimensions. A 5 by 5 PBM processed with a 3 by 3 rectangle produces a 5 by 5 PGM, not a 3 by 3 image.

3. Convert a PBM to a new PGM

With an existing input at /path/to/line-art.pbm, write the command's standard output to a new destination:

$ pbmtopgm 3 3 /path/to/line-art.pbm > line-art-soft.pgm

A successful conversion normally prints no progress message. Check the exit status immediately and inspect the result:

$ printf 'exit status: %s\n' "$?"
exit status: 0
$ file line-art-soft.pgm
line-art-soft.pgm: Netpbm image data, size = 5 x 5, rawbits, greymap

Your file wording may differ, but look for a PGM greymap and the expected input dimensions. The raw PGM header starts with P5. Do not use a text editor to inspect the whole result: the header is text, followed by binary sample data.

Safety checkpoint

Shell redirection with > truncates an existing destination before pbmtopgm starts. If line-art-soft.pgm already matters, use a temporary name and replace it only after checking the conversion:

$ pbmtopgm 3 3 /path/to/line-art.pbm > line-art-soft.pgm.new
$ test -s line-art-soft.pgm.new && file line-art-soft.pgm.new
line-art-soft.pgm.new: Netpbm image data, size = 5 x 5, rawbits, greymap
$ mv line-art-soft.pgm.new line-art-soft.pgm

The final mv changes the destination name, so use it only after the new file has passed your checks. If conversion fails, leave the original destination alone and investigate the error. Remove an unneeded temporary file later, once you are sure it is not the only copy of useful output.

4. Use standard input and output in a pipeline

Omit the PBM file argument when another command supplies the image:

$ cat /path/to/line-art.pbm | pbmtopgm 3 3 > line-art-soft.pgm

This is equivalent to passing the file directly, although the direct form is shorter and avoids an unnecessary cat process. More generally, keep diagnostics separate from image data: standard output is the PGM stream, while errors go to standard error. Do not capture both streams into the image file.

PBM may be in raw form, identified by P4, or plain form, identified by P1. Netpbm reads both forms. The conventional .pbm suffix is useful for people, but it is the file contents, not the suffix, that identify the image.

5. Choose a useful smoothing size

Start with 3 3 for thin line features. It usually softens jagged edges without spreading them too far. A larger value such as 5 5 averages a wider area and can make the result more blurred. Compare separate output files rather than overwriting your first result:

$ pbmtopgm 3 3 /path/to/line-art.pbm > line-art-3x3.pgm
$ pbmtopgm 5 5 /path/to/line-art.pbm > line-art-5x5.pgm
$ file line-art-3x3.pgm line-art-5x5.pgm
line-art-3x3.pgm: Netpbm image data, size = 5 x 5, rawbits, greymap
line-art-5x5.pgm: Netpbm image data, size = 5 x 5, rawbits, greymap

The exact grey values depend on the local PBM pixels and the chosen rectangle. If a later tool still needs smoother output, the manual suggests considering a separate pnmsmooth step. That is another transformation, so verify its output independently.

6. Diagnose failed conversions

If the command cannot open the input, check the path and read permission without changing the image:

$ ls -l /path/to/line-art.pbm
$ test -r /path/to/line-art.pbm && echo readable

If the input is not a valid PBM, inspect its first bytes and use a Netpbm reader to validate it. A normal PBM begins with P1 or P4, followed by width and height. A file merely named .pbm is not enough.

If the output is still only black and white, check the rectangle. A one-pixel width and height cannot create intermediate levels. If the output dimensions are unexpected, check the input header: pbmtopgm averages pixels but does not resize the image.

Keep the original PBM until the PGM has been inspected in a trusted viewer or passed through a later conversion. This workflow changes no source data, and there is no persistent configuration to undo. Recovery from a bad result is simply to discard that new PGM and run the command again with the original PBM and a different rectangle.

Done means

  • pbmtopgm is the installed Netpbm command you intended to use.
  • The width and height describe an averaging rectangle, preferably with odd values.
  • The new file is a non-empty PGM with the input image's dimensions.
  • An existing useful output was protected from redirection truncation.
  • The original PBM remains available for another conversion.