Home / Alt manpages / pamedge(1)

  • pamedge(1)
  • User command
  • linux

Use pamedge to Extract Image Edges in Netpbm

You will finish with a repeatable command-line workflow for turning a PNM or PAM image into an outline of its edges. You will also know how to save the result, make a bilevel mask when needed, and avoid the two easy mistakes: overwriting the source and mistaking a successful command for a useful image.

Allow about fifteen minutes. You need the Netpbm package, a readable PNM or PAM image, and enough disk space for one or two output files. The examples use Netpbm 11.5.2 from Debian package 2:11.05.02-1.1build1. Other releases may format diagnostics differently, but the installed manual and the official pamedge documentation describe the same one-file interface.

1. Check the installed command

Start with read-only checks. They do not need elevated privileges:

$ command -v pamedge
/usr/bin/pamedge
$ pamedge --version
pamedge: Using libnetpbm from Netpbm Version: Netpbm 11.5.2
...

The version output includes build details after the release number. Keep the full output when reporting a problem. This guide uses the command supplied by the installed package, not a separately downloaded binary.

Checkpoint: if command -v prints nothing, stop here and install Netpbm through your normal package-management process. Do not work around a missing command by downloading an unverified executable.

2. Run edge detection without changing the source

pamedge accepts one optional image file. With no file argument it reads standard input, and it writes the processed image to standard output. Redirect that output to a new name:

$ pamedge /path/to/input.pnm > /path/to/input-edges.pnm
$ file /path/to/input-edges.pnm
/path/to/input-edges.pnm: Netpbm image data, ...

The exact description from file depends on the input format and dimensions. A zero exit status means that pamedge completed and produced output. It does not tell you whether the outline is visually useful, so open the result in an image viewer or inspect it with a Netpbm tool before deleting the source.

Shell redirection truncates its destination before pamedge starts. Never use a source path as the destination path. If an output file already exists, preserve it first or choose a fresh name:

$ cp --preserve=all /path/to/input-edges.pnm /path/to/input-edges.pnm.bak
$ pamedge /path/to/input.pnm > /path/to/input-edges.pnm.new
$ mv /path/to/input-edges.pnm.new /path/to/input-edges.pnm

The temporary file is replaced only after pamedge has finished. If the command fails, leave the original output in place and remove the incomplete .new file after checking it. The backup is an ordinary file, so deleting it later with rm is irreversible.

3. Use a pipeline when the image is already on standard input

Netpbm programs are designed to connect through pipes. This is useful when an earlier converter produces PNM or PAM data:

$ upstream-converter /path/to/source-image > /tmp/source.pnm
$ pamedge /tmp/source.pnm > /path/to/source-edges.pnm

Replace upstream-converter with a real program that produces a Netpbm image. Do not treat the placeholder as a pamedge option. You can also pipe directly when the producer is trusted and its output format is known:

$ upstream-converter /path/to/source-image | pamedge > /path/to/source-edges.pnm

For a failure in a multi-stage pipeline, inspect the producer and pamedge separately. A shell may report only the status of the last command unless you enable a shell-specific pipeline-status setting. Keeping an intermediate file is often easier to diagnose and costs little for a short test.

4. Understand the output format and contrast

The edge calculation uses two perpendicular Sobel gradients and combines them as a Pythagorean magnitude. In practical terms, it emphasises changes in brightness rather than drawing a vector outline around objects. Flat areas should remain dark; texture, noise and sharp changes can all become edges.

The output normally keeps the input image type. There are two documented exceptions: output has a maximum sample value of at least 255, and PBM input becomes PGM output. The maximum sample value, or maxval, is the largest brightness number the format can store. If the input has a small maxval, increase it before edge detection:

$ pamdepth 255 /path/to/input.pnm > /path/to/input-8bit.pnm
$ pamedge /path/to/input-8bit.pnm > /path/to/input-edges.pnm

pamdepth rescales sample values and can introduce rounding when reducing depth. Keep the original and compare the result before using this in a batch. A larger maxval can give pamedge more useful brightness resolution, but it cannot recover detail that was already lost.

5. Produce a black-and-white edge mask

pamedge produces a greyscale edge image, not a PBM mask for ordinary PGM or PAM input. To turn it into a simple thresholded result, pipe it through pamditherbw -threshold, then use pamtopnm if a legacy PBM file is required:

$ pamedge /path/to/input.pnm \
    | pamditherbw -threshold \
    | pamtopnm > /path/to/input-edge-mask.pbm
$ file /path/to/input-edge-mask.pbm
/path/to/input-edge-mask.pbm: Netpbm image data, bitmap

The threshold step makes a hard decision from the greyscale values. Very faint edges may disappear, while noise may become isolated white pixels. The output of pamditherbw is PAM with a black-and-white tuple type; pamtopnm converts it to PBM for older consumers. If your next program understands PAM, omit the last stage and give the output a .pam name.

Checkpoint: inspect both the greyscale file and the mask. If the mask is too sparse or too noisy, adjust the source contrast or use a different thresholding workflow rather than repeatedly overwriting the same file.

6. Diagnose the common failures

If pamedge says it cannot open the image, check the path and permission without changing anything:

$ test -r /path/to/input.pnm && echo readable
$ ls -l /path/to/input.pnm

If the input is a JPEG, PNG or another non-Netpbm format, convert it with a suitable trusted converter first. pamedge itself documents PNM and PAM input, not arbitrary image formats. If conversion needs access to a protected directory, fix the file access or copy a permitted working copy. Do not run the whole image pipeline as root just to avoid checking permissions.

If the output is black, noisy or apparently unchanged, check the input's dimensions, format and maxval. A successful exit status cannot distinguish a poor source image from a good edge result. Try the greyscale output before the bilevel pipeline, and keep each stage in a separate file while investigating.

There is no persistent pamedge configuration to undo. The command reads an image and writes a new stream. Recovery means restoring the previous output from the backup, or deleting only an explicitly identified temporary file. It does not require a service restart or elevated privileges.

Done means

  • You confirmed the installed Netpbm and pamedge versions.
  • You supplied a readable PNM or PAM image and wrote to a different output path.
  • You checked the output file and visually inspected the edge result.
  • You understand that PBM input becomes PGM and output maxval is at least 255.
  • You used pamdepth only when higher sample resolution was useful.
  • You created a PBM edge mask only through an explicit thresholding pipeline.
  • Your original image and any useful previous output remain recoverable.