Home / Alt manpages / pambackground(1)

  • pambackground(1)
  • User command
  • linux

Make a Usable Background Mask with Netpbm pambackground

You will turn a PNM or PAM image with a flat, edge-connected background into a one-bit mask. In the mask, background pixels are 1 and foreground pixels are 0. The examples use Netpbm 11.5.2, take about five minutes, and do not require elevated privileges.

Before you start

Have an image in PNM or PAM format, such as a PPM photograph or a PGM drawing. The foreground should be a solid object placed over one colour. Its background must reach at least one image edge. This is a segmentation rule, not an edge detector: a foreground object with holes can expose background pixels and give a surprising result.

Keep the original. The command only reads its input, but the output path can still overwrite an existing file if you choose one carelessly. The commands below write to a new file under /tmp; change that path to a workspace you control when the result is ready to keep.

1. Create the mask

Pass the input filename after the command, or use standard input with -. Save the PAM output explicitly so a later command cannot accidentally consume it before you inspect it.

pambackground /path/to/source.ppm > /tmp/background-mask.pam

For a pipeline, the equivalent form is:

some-netpbm-filter /path/to/source.ppm | pambackground - > /tmp/background-mask.pam

There is no progress display by default. Add -verbose when you need to see the selected background colour and pass count.

pambackground -verbose /path/to/source.ppm > /tmp/background-mask.pam

On the installed version, a suitable test image reports a line like this on standard error:

pambackground: Background color is Blue
pambackground: Background found in 1 passes

The diagnostic uses the program's spelling shown above. It is diagnostic text, not a promise that every input will produce a named colour.

Checkpoint: inspect the output type

Confirm that the result is a PAM with one plane and maxval 1 before connecting it to another image operation.

pamfile /tmp/background-mask.pam

Expected output has this shape:

/tmp/background-mask.pam: PAM, 5 by 5 by 1 maxval 1
    Tuple type:

The dimensions will match your input. A sample value of 1 marks background; 0 marks foreground. The empty tuple type is normal for this mask, but it matters when handing the file to older PNM-only programs.

2. Convert the mask when a PAM consumer is unavailable

Netpbm tools generally understand PAM, but some older programs expect PBM or PGM. Convert to PBM with pamtopnm -assume. The option is deliberate: this mask has depth one but no tuple type, so pamtopnm otherwise refuses it.

pamtopnm -assume /tmp/background-mask.pam > /tmp/background-mask.pbm
pamfile /tmp/background-mask.pbm

Verification should identify a PBM image with the same dimensions, for example:

/tmp/background-mask.pbm: PBM raw, 5 by 5

Use pgmtopgm instead if the next program specifically needs PGM. Do not invert the mask unless the consuming option expects foreground rather than background. If you do need the inverse, write a separate file so the original mask remains available:

pnminvert /tmp/background-mask.pbm > /tmp/foreground-mask.pbm

How background selection works

pambackground first chooses a background colour from the corners. Three matching corners win. If only two match, it uses the matching pair in top, right, left, then bottom priority. If no pair matches, the upper-left corner supplies the colour.

It then finds pixels of that colour connected to an image edge. Pixels of the same colour enclosed by the foreground are not treated as background. This makes the tool useful for a solid logo on a flat canvas, but unsuitable for a foreground with transparent-looking holes when those holes should also be removed.

A photograph usually contains many near-identical background shades rather than one exact colour. In that case, the strict colour comparison will leave much of the background as foreground. Reduce the image to a small palette first, or normalise similar shades to one colour with other Netpbm tools, then run pambackground on that derived image. Keep the original image for the final compositing step.

pnmquant 5 /path/to/source.ppm | pambackground - > /tmp/quantised-background-mask.pam
pamfile /tmp/quantised-background-mask.pam

Palette reduction can also merge a foreground shade with the background. Inspect the mask rather than assuming that a visually close palette is safe. If the boundary is still wrong, use a deliberately constructed palette with pnmremap, or use ppmcolormask when you know the exact colour and need a simple colour mask.

3. Use the mask for compositing

A common next step is replacing the original background with another image. The mask identifies the background, so pass it as the alpha mask to pamcomp:

pamcomp -alpha=/tmp/background-mask.pam /path/to/source.ppm /path/to/new-background.ppm > /tmp/composited.ppm
pnmfile /tmp/composited.ppm

Check the composite dimensions and open it with an image viewer before deleting intermediate files. For transparency in an output format that supports it, the mask can be combined with image channels using Netpbm's PAM tools before conversion. The exact converter and channel arrangement depend on the target format; the mask's meaning remains background 1 and foreground 0.

Common traps and recovery

  • The mask is almost all zero. Check the corner colours and whether the intended background is one exact value. Quantise or normalise a copy, then repeat.
  • The wrong corner wins. Remember the corner-selection priority. Crop or prepare a copy whose corners represent the intended background; do not alter the source unless that is part of your workflow.
  • Holes remain unmasked. That is expected for a solid, edge-connected foreground assumption. Use ppmcolormask for a known unique background colour, or choose a tool designed for transparency-aware segmentation.
  • Conversion fails. Use pamtopnm -assume for this tuple-less one-plane PAM. If a different consumer rejects PBM, try the PGM conversion instead.
  • You overwrote a result. Re-run from the unchanged source into a new filename. The command has no undo store, so recovery of an overwritten file depends on your backup or filesystem recovery process.

Done means

  • The source was a PNM or PAM image with a suitable flat, edge-connected background.
  • pambackground produced a PAM whose dimensions match the source.
  • pamfile confirmed one plane and maxval 1.
  • You converted with -assume only when the next tool needed PBM or PGM.
  • You inspected the mask and kept the original source before compositing or cleanup.