Replace pnmcut Safely with pamcut for Netpbm Image Cropping
You will finish with a checked replacement for old pnmcut commands: pamcut, the Netpbm tool that cuts PAM, PBM, PGM and PPM images. The examples use the installed Netpbm 11.5.2 package and keep the original image untouched.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about fifteen minutes. You need a shell, Netpbm, and a readable image in one of the supported Netpbm formats. This guide does not need sudo. It 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 that 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. 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 that 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. For example, specifying both -cropright and -right 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 that mixed specifications are interpreted in an unspecified way. 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. That 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.
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 pamcutand ask your normal package administrator to install Netpbm. Do not usesudomerely to crop a file. - Cannot read the input: check
ls -l /path/to/input.ppmandtest -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 that a successful exit proves the crop is visually correct.
Done means
- New commands use
pamcut, not the obsoletepnmcutname. - The installed Netpbm version and input dimensions were checked.
- The crop uses explicit, zero-based coordinates or explicit edge counts.
- The output dimensions were verified with
file. -padwas used only when black out-of-bounds padding was intended.- The source remained intact, and an existing destination was replaced only after verification.