LinuxCommandLibrary

pnmgamma

Adjust the gamma of a PNM image

TLDR

Convert the image from BT.709 luminance to radiance or sRGB luminance

$ pnmgamma -[bt709tolinear|bt709tosrgb] [path/to/image.pnm] > [path/to/output.pnm]
copy

Convert the image from radiance or sRGB luminance to BT.709 luminance
$ pnmgamma -[lineartobt709|srgbtobt709] [path/to/image.pnm] > [path/to/output.pnm]
copy

Specify the gamma value used for the gamma transfer function
$ pnmgamma [[-ga|-gamma]] [value] [path/to/image.pnm] > [path/to/output.pnm]
copy

Specify the gamma value used for the gamma transfer function per color component
$ pnmgamma [[-rg|-rgamma]] [value] [[-gg|-ggamma]] [value] [[-bg|-bgamma]] [value] [path/to/image.pnm] > [path/to/output.pnm]
copy

SYNOPSIS

pnmgamma [-gamma=G] [-ungamma] [pnmfile]

PARAMETERS

-gamma=G
    Specifies the gamma value to apply. The input pixel values are transformed by raising them to the power of 1/G. A typical value for G is 2.2, which is also the default if neither -gamma nor -ungamma is specified. Setting G=1.0 results in no change to the image.

-ungamma
    Applies the inverse gamma correction. Input pixel values are transformed by raising them to the power of G. If -gamma=G is also specified, that G value is used; otherwise, the default G of 2.2 is applied. This mode is useful for linearizing an image before other processing, after which normal gamma correction can be reapplied.

pnmfile
    The path to the input PNM image file. If omitted, pnmgamma reads the image data from standard input (stdin).

DESCRIPTION

pnmgamma is a utility from the Netpbm image processing toolkit designed to apply gamma correction to PNM (Portable Anymap) image files. Gamma correction is a non-linear operation that maps pixel values to new values using a power law, typically expressed as V_out = V_in^(1/G), where G is the gamma value. This process is crucial for compensating for the non-linear response of display devices (like CRT monitors or modern LCDs) which tend to display an input value V_in as V_in^G. By applying gamma correction with a specific G, pnmgamma ensures that the image appears perceptually correct on a standard display.

The command reads a PNM image from standard input or a specified file and writes the modified image to standard output. It's often used to adjust the brightness and contrast of the midtones without significantly affecting the highlights or shadows. The default gamma value assumed by pnmgamma (if not specified) is typically 2.2, a common gamma value for many display devices.

CAVEATS

Gamma correction is a lossy operation, especially for images with low bit depths, due to floating-point calculations and subsequent rounding of pixel values. Repeated applications of gamma correction can degrade image quality. It's generally best to apply it once at the end of a processing chain, or to linearize the image first if complex manipulations are needed.

The term 'gamma' itself can be confusing; pnmgamma implements the display-compensation gamma (raising pixel values to 1/G), meaning a higher G brightens the image to compensate for a darker display. If you intend to make an image inherently darker for other reasons, you might need a different approach or a gamma value less than 1 (which would also brighten the image).

UNDERSTANDING GAMMA CORRECTION

Gamma correction is essential because human vision perceives brightness logarithmically, and display devices output light non-linearly. Most displays have a native gamma response (power law) of approximately 2.2 to 2.4. To ensure an image appears as intended, an inverse gamma correction (often called 'gamma encoding' or 'gamma compression') is applied to the image data before it's displayed. pnmgamma with -gamma=2.2 performs the appropriate operation (V_out = V_in^(1/2.2)) to make an image look correct on such a display.

Conversely, if you're trying to process an image that has already been 'gamma-encoded' (e.g., from a digital camera), you might want to linearize it first using -ungamma before performing operations like resizing, blending, or color space conversions. This prevents incorrect results due to non-linear averaging of pixel values. After processing, you'd re-apply standard gamma correction.

EXAMPLE USAGE

To apply a standard gamma correction of 2.2 to an image named input.ppm and save it as output.ppm:
pnmgamma -gamma=2.2 input.ppm > output.ppm

To linearize an image before processing it with another Netpbm tool, and then re-apply gamma:
cat input.ppm | pnmgamma -ungamma | pnmflip -lr | pnmgamma -gamma=2.2 > output.ppm
(This example linearizes, flips left-right, then re-applies gamma)

HISTORY

pnmgamma is a part of the Netpbm project, a comprehensive suite of graphics file format converters and image processing tools. Netpbm originated from Jef Poskanzer's PBMplus package in the early 1990s, evolving into the robust toolkit used today. The commands in the Netpbm suite, including pnmgamma, are known for their simplicity, adherence to the Unix philosophy (do one thing well), and their ability to be chained together using pipes. While specific development milestones for pnmgamma are not widely documented, it has been a foundational utility for basic image gamma adjustment within the Netpbm ecosystem for decades.

SEE ALSO

pnm(5), pnmnorm(1), pnmhisteq(1), xgamma(1)

Copied to clipboard