LinuxCommandLibrary

pnmarith

Perform arithmetic operations on Netpbm images

TLDR

View documentation for the current command

$ tldr pamarith
copy

SYNOPSIS

pnmarith operation pnmfile1 pnmfile2
pnmarith blend factor pnmfile1 pnmfile2

PARAMETERS

add
    Adds the pixel values of the two input images. The result is clamped to the maximum pixel value.

subtract
    Subtracts the pixel values of pnmfile2 from pnmfile1. Results are clamped at zero for negative values.

multiply
    Multiplies the pixel values of the two input images. The result is scaled down to fit the valid pixel range.

divide
    Divides the pixel values of pnmfile1 by pnmfile2. Division by zero results in a pixel value of zero.

min
    Selects the minimum pixel value between corresponding pixels of the two input images. Also known as darken.

max
    Selects the maximum pixel value between corresponding pixels of the two input images. Also known as lighten.

difference
    Calculates the absolute difference between corresponding pixel values. Also known as absdiff.

average
    Computes the arithmetic average of the corresponding pixel values from the two images.

blend factor
    Blends the two images. factor is a floating-point number between 0.0 and 1.0. A factor of 0.0 means 100% of pnmfile1, 1.0 means 100% of pnmfile2.

DESCRIPTION

pnmarith is a command-line utility from the Netpbm suite that applies various arithmetic operations to corresponding pixels of two input Portable Anymap (PNM) images. This includes bitmap (PBM), grayscale (PGM), and pixmap (PPM) formats. It's an essential tool for combining images, creating overlays, performing pixel-level analysis, or adjusting image brightness/contrast based on another image's data. Operations are performed channel-by-channel for color images. The command intelligently handles differences in image dimensions (requiring them to be the same), color depths, and maximum pixel values to produce a valid output PNM image.

CAVEATS

Images must have identical dimensions (width and height). If input images have different maximum pixel values (maxval), pnmarith automatically scales the lower maxval image to match the higher before computation. The output image will adopt the higher maxval. All arithmetic results are clamped to be within the valid pixel range (0 to maxval).

INPUT AND OUTPUT

pnmarith reads its two input images from the filenames specified on the command line. A hyphen (-) can be used in place of a filename to indicate reading from standard input. The resulting image is always written to standard output.

COLOR AND MAXVAL HANDLING

For color images (PPM), operations are performed independently on each of the Red, Green, and Blue color components. For grayscale (PGM) or bitmap (PBM) images, the operation applies to the single pixel value. If input images have different maximum pixel values (e.g., 255 vs 65535), the image with the lower maxval is internally scaled up to match the higher maxval before calculations, ensuring consistent arithmetic results. The output image will have the higher maxval.

HISTORY

pnmarith is a long-standing component of the Netpbm package, which originated from Jef Poskanzer's pbmplus suite in the late 1980s. It embodies the Unix philosophy of doing one thing well, providing a simple yet powerful command-line interface for fundamental image arithmetic. While more modern and flexible tools like pamarith have been introduced within Netpbm to handle arbitrary tuple types, pnmarith remains a standard and widely used utility for its direct approach to PNM image manipulation.

SEE ALSO

pnm(5), pamarith(1), netpbm(1), pnmgamma(1)

Copied to clipboard