Home / Alt manpages / pnmcut(1)

  • pnmcut(1)
  • User command
  • linux

Replace pnmcut Safely with pamcut for Netpbm Image Cropping

pnmcut still cuts a rectangle out of a Netpbm image, but the name is retired: pamcut is the tool that replaced it for PAM, PBM, PGM and PPM images. The examples use the installed Netpbm 11.5.2 package and keep the original image untouched.

  • You need: a shell, Netpbm, and a readable image in one of the supported Netpbm formats.
  • No sudo needed. This guide does not alter package configuration, image metadata or the source file.

1. Check the installed tools

The local pnmcut(1) manual is deliberately short: it says pnmcut was replaced by pamcut. The installed command is still present for compatibility, but new scripts should name the maintained interface explicitly:

$ command -v pnmcut pamcut
/usr/bin/pnmcut
/usr/bin/pamcut
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
$ pamcut --version 2>&1 | head -n 1
pamcut: Using libnetpbm from Netpbm Version: Netpbm 11.5.2

Checkpoint

Both commands may exist, but pamcut is the name to put in new shell scripts. The version line confirms which local implementation you are testing, rather than relying on a distribution-independent assumption.

2. Inspect the source image before cutting

Use file to establish the input dimensions and format. Replace the placeholder with your own path:

$ file /path/to/input.ppm
/path/to/input.ppm: Netpbm image data, size 1600 x 1200, rawbits, pixmap

The wording varies by file type and by the file package version. You are looking for a readable Netpbm image and the dimensions, so do not infer coordinates from a filename.

Keep a source copy until you have inspected the result. Shell redirection with > truncates an existing destination before pamcut starts, so choose a new output name for the first run.

3. Cut a rectangle by position and size

Use -left and -top for the first column and row, then -width and -height for the rectangle size. Coordinates start at zero, so the top-left pixel is column 0, row 0:

$ pamcut -left 100 -top 50 -width 800 -height 600 \
    /path/to/input.ppm > /path/to/output-800x600.ppm
$ file /path/to/output-800x600.ppm
/path/to/output-800x600.ppm: Netpbm image data, size 800 x 600, rawbits, pixmap

The four values describe what to keep, not how many pixels to remove. -width and -height must be positive. If you accidentally request a rectangle outside the input, the command fails unless you explicitly add -pad.

Checkpoint

Verify the dimensions before deleting or replacing anything. A successful exit status only says the command completed; file confirms the shape you intended.

4. Crop edges when the margins are what you know

If the requirement is to discard a known number of pixels from each edge, use the crop options: this is often clearer than calculating a right coordinate.

$ pamcut -cropleft 20 -cropright 20 \
    -croptop 10 -cropbottom 10 \
    /path/to/input.ppm > /path/to/trimmed.ppm
$ file /path/to/trimmed.ppm
/path/to/trimmed.ppm: Netpbm image data, size 1560 x 1180, rawbits, pixmap

Crop counts cannot be negative. These options were added in Netpbm 10.85, so they are available in the installed 11.5.2 build. Do not combine a crop option with a conflicting edge selector: specifying both -cropright and -right, for example, overspecifies the right edge and is an error.

5. Convert an existing pnmcut command

The old positional form is still documented for compatibility, but options are easier to audit in a script. Convert a command such as this:

$ pnmcut 100 50 800 600 input.ppm > output.ppm

to this equivalent, more legible form:

$ pamcut -left 100 -top 50 -width 800 -height 600 \
    input.ppm > output.ppm

Do not mix positional arguments and options in one invocation. The pamcut manual says mixed specifications are interpreted in an unspecified way, so update one script at a time, then run its normal verification step.

For a pipeline, omit the input filename and feed the image on standard input:

$ cat input.ppm | pamcut -left 100 -top 50 -width 800 -height 600 > output.ppm
$ test -s output.ppm && file output.ppm

The same output convention applies: image data goes to standard output. Keep diagnostics separate by leaving standard error visible.

6. Handle padding as an explicit decision

By default, an out-of-bounds rectangle is an error, which protects you from silently creating a result with an unintended size. Add -pad only when black padding outside the source is part of the requirement:

$ pamcut -left -40 -top -40 -width 800 -height 600 -pad \
    /path/to/input.ppm > /path/to/padded.ppm
$ file /path/to/padded.ppm
/path/to/padded.ppm: Netpbm image data, size 800 x 600, rawbits, pixmap

Negative coordinates count backwards from the right or bottom edge, while coordinates beyond the image can require padding. Treat this as a visual change, not a harmless error workaround: the added area is black. If you need a coloured or composited frame, use a tool such as pamcomp instead.

Recovery

To undo a mistaken output, remove only the generated file after checking its exact path, or restore the previous file from your backup. The source image is not changed by pamcut. If you must replace an existing output, write to a temporary name, verify it, then move it into place:

$ pamcut -left 100 -top 50 -width 800 -height 600 \
    input.ppm > output.ppm.new
$ file output.ppm.new
$ mv output.ppm.new output.ppm

The final mv is the state-changing step. Do not run it until the dimensions and, where possible, the image contents are correct.

7. Diagnose the likely failures

  • Command not found: check command -v pamcut and ask your normal package administrator to install Netpbm. Do not use sudo merely to crop a file.
  • Cannot read the input: check ls -l /path/to/input.ppm and test -r /path/to/input.ppm. Fix the path or permissions deliberately.
  • Invalid or empty output: inspect the exit status and run file. Check for reversed coordinates, zero dimensions, or a rectangle outside the image.
  • Unexpected black borders: look for -pad. Remove it when an exact in-image crop is required.
  • A script still calls pnmcut: replace the command name, preserve its verified coordinates, and test the resulting file. Do not assume a successful exit proves the crop is visually correct.

Done means

  • Command updated. New commands use pamcut, not the obsolete pnmcut name.
  • Version and dimensions checked. The installed Netpbm version and input dimensions were checked.
  • Coordinates explicit. The crop uses explicit, zero-based coordinates or explicit edge counts.
  • Output verified. The output dimensions were verified with file.
  • Padding intentional. -pad was used only when black out-of-bounds padding was intended.
  • Source protected. The source remained intact, and an existing destination was replaced only after verification.