Home / Alt manpages / pamsumm(1)

  • pamsumm(1)
  • User command
  • linux

Measure Netpbm Image Samples with pamsumm

You will use pamsumm to calculate a total, arithmetic mean, minimum or maximum across every sample in a Netpbm image. The examples use a small PGM file, then show how to normalise values and connect the command to another Netpbm tool. Allow about ten minutes. You need the netpbm package and a readable PNM or PAM image.

The installed command used for these checks is Netpbm 11.5.2, from package version 2:11.05.02-1.1build1. Package versions on other systems can differ, so check the local executable before relying on output wording in a script.

1. Check the executable and make a test image

First confirm which executable your shell will run:

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

The version command prints additional build information on this installation. The manual page describes pamsumm as operating on PNM or PAM input. For a repeatable check, create a temporary plain PGM containing four samples:

$ cat > sample.pgm <<'EOF'
P2
2 2
10
0 5
10 10
EOF

This is an ordinary user-level file operation. It does not need sudo, and it does not alter an existing image. The pixels have a maximum sample value, or maxval, of 10; their values are 0, 5, 10 and 10.

2. Choose exactly one calculation

Run one of the four operation options. The input filename may be supplied at the end. With no filename, the command reads standard input, which is useful in a pipeline.

$ pamsumm -sum sample.pgm
the sum of all samples is 25
$ pamsumm -mean sample.pgm
the mean of all samples is 6.250000
$ pamsumm -min sample.pgm
the minimum of all samples is 0
$ pamsumm -max sample.pgm
the maximum of all samples is 10

The four results match the input: 0 + 5 + 10 + 10 is 25, and 25 divided by four is 6.25. The operation covers all rows, columns and planes. A colour image is therefore not treated as one brightness value per pixel; its individual channel samples are included.

You must provide exactly one of -sum, -mean, -min or -max. If you omit the operation, or try to combine two, fix the command rather than guessing what the default might be. The options can be abbreviated to a unique prefix, but full names are clearer in shared scripts.

Checkpoint: if the command succeeds, its exit status is zero. Capture it immediately if a script needs to distinguish a successful calculation from a read failure:

$ pamsumm -mean sample.pgm > result.txt
$ status=$?
$ test "$status" -eq 0 && echo "calculation succeeded"
calculation succeeded
$ cat result.txt
the mean of all samples is 6.250000

3. Produce a machine-friendly number

Human-readable output is useful at a terminal, but a script may want only the result. Add -brief:

$ pamsumm -mean -brief sample.pgm
6.250000

This option changes presentation only. It does not change the calculation or the precision shown. Do not parse the normal sentence by assuming its wording is stable across package versions; use -brief when a bare number is the intended interface.

Keep the error stream separate from a captured result. A missing or unreadable image is an error, not a number:

$ pamsumm -mean -brief /path/to/missing.pgm > result.txt
pamsumm: Unable to open file '/path/to/missing.pgm' for reading. ...
$ test ! -s result.txt && echo "no result was accepted"
no result was accepted

The precise diagnostic includes the operating system error and can vary. Check the input path with ls -l and test -r before changing permissions or running as root.

4. Normalise values when maxval should not matter

Without -normalize, the arithmetic uses the stored sample values. An image with samples of 50 and maxval 200 therefore has a mean of 50. Add -normalize to convert every sample to a fraction from 0 to 1 before calculating:

$ cat > half.pgm <<'EOF'
P2
2 1
200
50 50
EOF
$ pamsumm -mean -normalize half.pgm
the mean of all samples is 0.250000

Here 50 divided by 200 is 0.25. Normalisation makes results comparable when equivalent images use different maximum values. It does not convert a visual image's stored samples into physical light intensity. For a brightness calculation based on light intensity, first use an appropriate gamma conversion such as pnmgamma, then run pamsumm.

If you want a common integer scale instead, convert the input to a chosen maxval with pamdepth and leave normalisation off:

$ pamdepth 99 half.pgm | pamsumm -mean
the mean of all samples is 25.000000

This pipeline reads the converted image from standard input. The displayed result is 25 because the two samples become 25 on a scale of 0 to 99. Make sure the conversion's scale matches what the receiving process expects.

5. Choose the right scope for the question

pamsumm gives one result for the complete image. To calculate separate column results, use pamsummcol. To work on one plane of a multi-plane image, extract it with pamchannel first. A row-by-row result needs pamsummcol with an appropriate pamflip step. These are different operations, so do not interpret one whole-image number as a per-channel or per-row report.

Remember that PGM, PPM and PAM samples have format-specific meanings. A sample value is not automatically a linear measure of displayed brightness. Check the format and the intended measurement before comparing means from unrelated files.

6. Finish without leaving a misleading result

Writing with > truncates an existing destination before pamsumm starts. That is harmless for a disposable report, but it can destroy a previous result. Use a new temporary name and move it into place only after success:

$ pamsumm -mean -brief sample.pgm > result.txt.new
$ status=$?
$ if test "$status" -eq 0; then
>     mv result.txt.new result.txt
> else
>     rm -f result.txt.new
>     exit "$status"
> fi
$ cat result.txt
6.250000

The final rm removes only the failed temporary report, not the original result. Review the path before using this pattern in a batch job. No command in this guide needs elevated privileges unless your own input or output directory is deliberately protected, and changing those permissions is outside the calculation itself.

Done means

  • You selected exactly one calculation: sum, mean, minimum or maximum.
  • You know whether the result uses stored samples or normalised fractions.
  • You used -brief when a script needs a bare number and checked the exit status separately.
  • You accounted for every sample across the image's rows, columns and planes.
  • You checked the input and protected any existing output from a failed or accidental overwrite.