LinuxCommandLibrary

pnminvert

Invert the colors in a PNM image

TLDR

Invert the colors or greyscale values in a PNM image

$ pnminvert [path/to/input.pnm] > [path/to/output.pnm]
copy

SYNOPSIS

pnminvert [pnmfile]

DESCRIPTION

pnminvert is a Netpbm tool designed to perform a color inversion on a Portable Anymap (PNM) image.
It operates by reading a PNM image from standard input (or a specified filename if provided as an argument) and then writes the inverted image to standard output.
For grayscale images (PGM), it flips the intensity values, meaning black becomes white, white becomes black, and intermediate gray shades are similarly inverted.
For color images (PPM), pnminvert processes each of the red, green, and blue color components independently, transforming the image into its photographic negative.
This utility is a fundamental component of the Netpbm toolkit, frequently used in command-line pipelines with other Netpbm commands for more complex image manipulation tasks.

CAVEATS

pnminvert strictly processes Portable Anymap (PNM) images (PBM, PGM, PPM). The input image must be in one of these formats.
If your source image is in a different format (e.g., JPEG, PNG, TIFF), you must first convert it to PNM using a suitable Netpbm converter (such as jpegtopnm or pngtopnm) before piping it to pnminvert.
As it works with standard input and output, it's highly efficient for piping, but requires proper redirection for file-based operations.

USAGE EXAMPLE

To invert an image file named original.pnm and save the resulting negative to negative.pnm:
pnminvert original.pnm > negative.pnm

To integrate pnminvert into a command pipeline (e.g., convert a JPEG to PNM, invert it, then convert to PNG):
jpegtopnm input.jpg | pnminvert | pnmtopng > output_inverted.png

HOW INVERSION WORKS

The inversion process for each individual pixel's color component (or grayscale value) is calculated using a simple formula: new_value = maxval - old_value.
In this formula, maxval represents the maximum pixel value defined for the image type (e.g., 255 for a typical 8-bit image, or 1 for a PBM (monochrome) image). This calculation ensures that a pixel value of 0 (e.g., black) becomes maxval (e.g., white), and maxval becomes 0, with all intermediate values being proportionally inverted.

HISTORY

pnminvert is a long-standing utility that forms part of the comprehensive Netpbm suite. The Netpbm project itself evolved from the PBMplus package, initially developed by Jef Poskanzer in the late 1980s. PBMplus established a foundational set of command-line tools for manipulating portable bitmaps, graymaps, and pixmaps.
Netpbm continued and expanded upon this legacy, becoming a widely adopted standard for command-line image processing on Unix-like operating systems. pnminvert has been a core component for basic image transformations since these early developments, underscoring its fundamental utility in image manipulation workflows.

SEE ALSO

pnm(5), pnmgamma(1), pnmdepth(1), pnmhisteq(1), pnmscale(1), netpbm(1)

Copied to clipboard