Replace Obsolete pnmarith Calls with pamarith
An old script calling pnmarith still runs, but it is quietly using the obsolete name for pamarith. There is one compatibility gap that can change your results. On this machine the netpbm package is version 2:11.05.02-1.1build1 and provides Netpbm 11.5.2. Allow about fifteen minutes if you already have two small Netpbm images to test with.
The route
Jump straight to the step you need, or tick off Done means at the end.
- You need: a shell, the
netpbmpackage, and readable PBM, PGM, PPM or PAM inputs. - No sudo needed unless your input or output directory has deliberately been locked down.
1. Confirm what pnmarith is on this host
Check the installed command rather than assuming an old script still runs the old implementation:
$ command -v pnmarith
/usr/bin/pnmarith
$ ls -l /usr/bin/pnmarith
lrwxrwxrwx 1 root root 8 Mar 31 2024 /usr/bin/pnmarith -> pamarith
$ dpkg-query -W -f='${Package} ${Version}\n' netpbm
netpbm 2:11.05.02-1.1build1
Here pnmarith is already a symbolic link to pamarith. The installed manual page still describes the historical interface: starting with Netpbm 10.3, pnmarith is obsolete and pamarith is the replacement. Keep the old name only as a compatibility clue; new scripts should call pamarith explicitly.
Checkpoint
Check the function syntax without changing a file:
$ pamarith --help
pamarith: Use 'man pamarith' for help.
The help message is brief, so the real contract is in man pamarith. One operation is required, followed by two or more input files: -add, -subtract, -difference, -minimum, -maximum, -mean, -equal and the bit operations. The command writes the calculated image to standard output.
2. Make a harmless two-image test
If you do not have test images, create two tiny PGM files in a temporary directory. They are disposable and never touch your project or image archive:
test_dir=$(mktemp -d /tmp/pamarith-test.XXXXXX)
printf 'P2\n2 1\n10\n2 8\n' > "$test_dir/left.pgm"
printf 'P2\n2 1\n10\n3 4\n' > "$test_dir/right.pgm"
printf 'test files: %s\n' "$test_dir"
Each image is two pixels wide and one pixel high, with a maximum sample value of 10. The arithmetic result is an image stream, not printed text, so redirect it to a new path, never to either input.
3. Replace the command name and verify the output
Run the supported name first. This adds the corresponding samples, so the expected values are 5 and 10:
$ pamarith -add "$test_dir/left.pgm" "$test_dir/right.pgm" > "$test_dir/sum.pgm"
$ file "$test_dir/sum.pgm"
/tmp/pamarith-test.XXXXXX/sum.pgm: Netpbm image data, size = 2 x 1, rawbits, greymap
$ pnmtoplainpnm "$test_dir/sum.pgm"
P2
2 1
10
5 10
- Check the status. A zero exit code.
- Check the shape. A 2 by 1 PGM result.
- Check the values. Sample values
5 10. The temporary directory suffix andfilewording will vary, and ifpnmtoplainpnmis not installed, inspect the header withheador another Netpbm reader instead; it is a separate inspection tool, not part ofpamarith.
Now prove that the old invocation is currently equivalent on this installation:
$ pnmarith -add "$test_dir/left.pgm" "$test_dir/right.pgm" > "$test_dir/old-name.pgm"
$ cmp "$test_dir/sum.pgm" "$test_dir/old-name.pgm"
$ printf 'same output\n'
same output
cmp producing no diagnostic means the files are byte-for-byte identical. Treat this as a migration check, not a reason to keep the obsolete spelling in new code.
4. Check the image-shape boundary before migrating a real job
The important difference is image depth. Historical pnmarith converted the input with the lesser depth up to the higher depth before doing arithmetic. pamarith requires equal depths, unless one image has depth one. A grayscale PGM has depth one and an RGB PPM has depth three, so that common pairing is accepted; other mismatches can fail:
$ pamarith -add depth-two.pam colour.ppm > result.pam
pamarith: The images must have the same depth or depth 1. But one has depth 2 and another 3
The exact diagnostic may differ by release, but a non-zero status means no result was produced. Do not guess at a conversion or silently drop channels: inspect both files with a Netpbm-aware tool first and decide what each tuple should mean.
5. Normalise a mismatched input deliberately
The pnmarith manual specifically points to pgmtoppm for the conversion step. Use it only when turning the grayscale input into an RGB image is actually what you want. This changes the intermediate file, so write a new one and keep the original until the result has been checked:
$ pgmtoppm '#808080' depth-two-source.pgm > normalised.ppm
$ pamarith -add normalised.ppm colour.ppm > result.ppm
That example is for a PGM source and produces an RGB PPM with the chosen background colour. It is not a universal repair for PAM images with arbitrary tuple types; for alpha, multi-channel scientific data or a service pipeline, choose a conversion that preserves the data model and test it against representative images.
Warning
Shell redirection with > truncates an existing destination before pamarith starts. Use a new name or a temporary output, then replace the old file only after validation:
set -o noclobber
pamarith -add "$test_dir/left.pgm" "$test_dir/right.pgm" > "$test_dir/sum.new.pgm"
set +o noclobber
test -s "$test_dir/sum.new.pgm" && mv "$test_dir/sum.new.pgm" "$test_dir/sum.pgm"
Recovery
If the conversion fails, leave the previous output alone and investigate the error. To undo a successful replacement, restore your own backup or regenerate the old output from its original inputs; the arithmetic command itself has no rollback operation.
6. Keep scripts explicit
Change only the command name in a straightforward script, then add an image-shape check where inputs can vary:
if ! pamarith -add "$left_image" "$right_image" > "$new_output"; then
printf 'pamarith failed; keeping the existing output\n' >&2
exit 1
fi
test -s "$new_output" || {
printf 'pamarith produced an empty output\n' >&2
exit 1
}
Matching width and height are also required: arithmetic runs at corresponding pixel positions, and the output format is the more general of the inputs. For a multi-input operation such as -add, the installed documentation permits repeated binary addition across multiple images, but subtraction and several comparison operations stay two-image operations.
Done means
- Version confirmed. You confirmed the installed Netpbm version and whether
pnmarithis an alias. - Scripts updated. New commands use
pamarithwith an explicit operation and output redirection. - Test matched. A small test produced the expected image and the old and new names matched byte-for-byte where the alias exists.
- Shape checked. You checked width, height, depth and tuple meaning before migrating real images.
- Conversion deliberate. Any depth conversion is written to a new file and verified before replacement.
- Failures are safe. Failed commands leave the previous output available and no elevated privilege is used unnecessarily.