LinuxCommandLibrary

pnmpsnr

Compute peak signal-to-noise ratio between images

TLDR

Compute the difference, i.e. the peak signal-to-noise ratio (PSNR) between two images

$ pnmpsnr [path/to/file1.pnm] [path/to/file2.pnm]
copy

Compare the color components rather than the luminance and chrominance components of the images
$ pnmpsnr [path/to/file1.pnm] [path/to/file2.pnm] -rgb
copy

Run in comparison mode, i.e. only output nomatch or match depending on whether the computing PSNR exceeds n or not
$ pnmpsnr [path/to/file1.pnm] [path/to/file2.pnm] -target [n]
copy

Run in comparison mode and compare the individual image components, i.e. Y, Cb, and Cr, to the corresponding thresholds
$ pnmpsnr [path/to/file1.pnm] [path/to/file2.pnm] -target1 [threshold_Y] -target2 [threshold_Cb] -target3 [threshold_Cr]
copy

Run in comparison mode and compare the individual image components, i.e. red, green, and blue to the corresponding thresholds
$ pnmpsnr [path/to/file1.pnm] [path/to/file2.pnm] -rgb -target1 [threshold_red] -target2 [threshold_green] -target3 [threshold_blue]
copy

Produce machine-readable output
$ pnmpsnr [path/to/file1.pnm] [path/to/file2.pnm] -machine
copy

SYNOPSIS

pnmpsnr [options] original_pnm_file new_pnm_file

PARAMETERS

-plain
    Output just the PSNR or MSE value, without any descriptive text.

-raw
    Equivalent to -plain.

-mse
    Output the Mean Squared Error (MSE) instead of PSNR.

-absolute
    Use absolute differences for MSE calculation instead of squared differences. This results in Mean Absolute Difference (MAD) or Peak Absolute Difference (PAD), not standard MSE/PSNR. This is a legacy feature for historical comparisons.

-maxval value
    Specify the maximum possible pixel value to use in PSNR calculation. By default, it uses the maxval from the images. This option is useful if the actual pixel range for comparison is different from the image's encoded maxval.

DESCRIPTION

pnmpsnr is a Netpbm program that computes the Peak Signal-to-Noise Ratio (PSNR) between two portable anymap (PNM) images. PSNR is a widely used metric for measuring the quality of reconstruction of lossy compressed images. It compares a compressed or processed image to its original uncompressed version.

The command reads two image files, which must have identical dimensions and format (PBM, PGM, or PPM). It calculates the Mean Squared Error (MSE) between the two images and then derives the PSNR value from it. A higher PSNR value indicates a better quality image, meaning less distortion or noise introduced. The result is printed to standard output. This tool is essential for evaluating image compression algorithms and assessing image degradation.

CAVEATS

Both input images must have identical dimensions and be of the same PNM type (PBM, PGM, or PPM).
The calculations are performed on the raw numeric pixel values; no gamma correction or color space transformations are applied.
Using the -absolute option deviates from the standard PSNR/MSE definition.

PSNR FORMULA

PSNR (Peak Signal-to-Noise Ratio) is typically defined as:

PSNR = 10 * log10((MAX_I^2) / MSE)

Where:
- MAX_I is the maximum possible pixel value of the image (e.g., 255 for 8-bit images).
- MSE is the Mean Squared Error between the two images.

MSE FORMULA

MSE (Mean Squared Error) is calculated as:

MSE = (1 / (M * N)) * sum((I1(i,j) - I2(i,j))^2)

Where:
- M and N are the image dimensions (width and height).
- I1(i,j) and I2(i,j) are the pixel values of the two images at coordinate (i,j).

HISTORY

pnmpsnr is part of the Netpbm project, a long-standing collection of graphics utilities for manipulating image files. Netpbm originated from the PBMPLUS package developed by Jef Poskanzer in the late 1980s. It provides a foundational set of tools for converting, processing, and analyzing images in the simple and versatile PNM (Portable Anymap) formats. pnmpsnr specifically addresses the need for quantitative image quality assessment, which became increasingly important with the rise of image compression technologies.

SEE ALSO

pnm(5), pgm(5), ppm(5), netpbm(1), pnmhisteq(1), identify(1) (ImageMagick), compare(1) (ImageMagick)

Copied to clipboard