pamarith
Perform arithmetic operations in parallel
TLDR
Apply the specified binary function pixel-wise on the two specified images (which must be of the same size)
SYNOPSIS
pamarith {-add|-sub|-mul|-div|-min|-max|-avg} {file1 | -} {file2 | constant | -} [-outputtype={pam|pgm|ppm}] [-tupletype=<string>]
PARAMETERS
-add
Performs addition. The result is the sum of corresponding pixel values from the two inputs.
-sub
Performs subtraction. The result is the value of the second input subtracted from the first.
-mul
Performs multiplication. The result is the product of corresponding pixel values.
-div
Performs division. The result is the first input's pixel value divided by the second. Division by zero results in the maximum possible pixel value.
-min
Returns the minimum of the corresponding pixel values from the two inputs.
-max
Returns the maximum of the corresponding pixel values from the two inputs.
-avg
Calculates the average of the corresponding pixel values from the two inputs.
file1, file2
Paths to the input Netpbm image files. Use '-' to read from standard input.
constant
A numeric value to be used in arithmetic operations with an image.
-outputtype={pam|pgm|ppm}
Specifies the output image format. Default is PAM.
-tupletype=<string>
Forces the tuple type of the output PAM image (e.g., 'GRAYSCALE', 'RGB'). Useful for overriding default type inference.
DESCRIPTION
pamarith is a Netpbm command-line tool designed for performing various arithmetic operations on image files. It can operate on two input images (e.g., image A + image B) or an image and a scalar value (e.g., image A * constant). The command supports operations like addition, subtraction, multiplication, division, finding the minimum or maximum pixel value between corresponding pixels of two images, and averaging. It works with all Netpbm formats: PGM (grayscale), PPM (color), PBM (bitmap), and PAM (an extensible format). The output is typically a PAM image by default, but can be specified to be PGM or PPM. pamarith is particularly useful for tasks like image blending, masking, comparing images, or adjusting brightness/contrast uniformly. It reads input from standard input if no file is specified and writes output to standard output, allowing for pipe-based workflows.
CAVEATS
Input images must have the same width and height for operations involving two images. If an operation involves division by zero, the resulting pixel value will be the maximum possible value for that pixel type. pamarith automatically handles differences in 'maxval' (maximum pixel value) and tuple types between input images by scaling or converting as necessary.
INPUT/OUTPUT
By default, pamarith reads its input image(s) from the specified file paths or from standard input (if '-' is used) and writes the resulting image to standard output. This design makes it highly compatible with shell piping for complex image processing workflows.
MAXVAL AND TUPLE TYPE HANDLING
pamarith is designed to intelligently handle images with differing 'maxval' (maximum pixel value) settings and tuple types. It automatically scales pixel values to a common range when operations involve images with different maxvals. Similarly, it attempts to combine images with different tuple types (e.g., an RGB image with a grayscale image) by performing appropriate conversions or promoting types to ensure a meaningful arithmetic operation.
HISTORY
pamarith is part of the Netpbm package and was introduced to replace the older, less flexible pnmarith command. While pnmarith was limited to PNM (PGM/PPM/PBM) formats, pamarith extends support to the more versatile PAM format, allowing for more complex image data and operations. Its development reflects the evolution of Netpbm towards a more robust and generalized image manipulation suite.