LinuxCommandLibrary

pnmrotate

Rotate Portable Anymap (PNM) image files

TLDR

Rotate a PNM image by some angle (measured in degrees, counter-clockwise)

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

Specify the background color exposed by rotating the input image
$ pnmrotate [[-b|-background]] [color] [angle] [path/to/input.pnm] > [path/to/output.pnm]
copy

Disable anti-aliasing, improving performance but decreasing quality
$ pnmrotate [[-n|-noantialias]] [angle] [path/to/input.pnm] > [path/to/output.pnm]
copy

SYNOPSIS

pnmrotate [-angle degrees] [-background color] [-bilinear | -point | -bicubic] [-c corner] [-exact] [-noantialias] [pnmfile]

PARAMETERS

-angle degrees
    The angle in degrees by which to rotate the image. Positive values rotate clockwise. The default is 0 degrees (no rotation).

-background color
    Specifies the color to fill in any newly exposed areas of the image after rotation. The color can be specified using pnm_parsecolor() format (e.g., "red", "#FF0000"). The default is black.

-bilinear
    Selects bilinear interpolation. This is the default method for arbitrary angle rotations, offering a good balance between speed and quality.

-point
    Selects point sampling (nearest neighbor) interpolation. This is the fastest method but can result in a blocky or pixelated appearance, especially for small angles.

-bicubic
    Selects bicubic interpolation. This method generally produces the highest quality results but is the slowest to process.

-c corner
    Specifies the corner or center point around which the image is rotated. Options include topleft, topright, bottomleft, bottomright, and center. The default is center.

-exact
    If specified, the output image dimensions are chosen precisely to contain the entire rotated image without cropping. This may result in larger dimensions with more background compared to the default behavior.

-noantialias
    Disables antialiasing for arbitrary angle rotations. This option is primarily useful when pnmrotate would use pnmgamma for antialiasing, potentially speeding up processing but reducing image smoothness.

pnmfile
    The input Netpbm image file. If not specified, pnmrotate reads the image data from standard input.

DESCRIPTION

pnmrotate is a versatile utility from the Netpbm image processing toolkit, designed to rotate Portable Anymap (PNM) format images. It reads an image from standard input (or a specified file) and outputs the rotated image to standard output. The command supports rotation by any specified angle, not just multiples of 90 degrees.

For rotations that are exact multiples of 90, 180, or 270 degrees, pnmrotate performs a lossless operation without interpolation, resulting in perfectly sharp output. However, for arbitrary angles, the tool employs various interpolation algorithms—bilinear by default, but also point sampling (nearest neighbor) or bicubic interpolation—to calculate the new pixel values. While interpolation allows for smooth rotation at any angle, it can introduce some degree of blurring or artifacts, depending on the chosen method. Newly exposed areas of the image, which were outside the original bounds but become visible after rotation, can be filled with a specified background color. pnmrotate is an essential tool for precise image orientation adjustments within workflows utilizing the Netpbm suite.

CAVEATS

pnmrotate's behavior for arbitrary angles relies on interpolation, which inherently involves approximation. This can lead to a slight loss of image sharpness or the introduction of artifacts, especially when rotating by non-integral angles or when using the faster but lower-quality point sampling method. Choosing the appropriate interpolation method (bilinear or bicubic) is crucial for visual quality but impacts processing time.

Additionally, the output image dimensions for arbitrary rotations are generally a bounding box that encapsulates the rotated image, which can be larger than the original, unless specific angles (multiples of 90 degrees) are used without the -exact option.

INTERPOLATION QUALITY TRADE-OFFS

When rotating images by arbitrary angles, pnmrotate relies on interpolation to calculate the new pixel values.
Point sampling (-point) is the fastest but can result in a blocky or jagged appearance, as it simply picks the nearest original pixel.
Bilinear interpolation (-bilinear, the default) provides a smoother result by averaging the four nearest pixels, balancing speed and quality.
Bicubic interpolation (-bicubic) offers the highest quality by considering 16 nearest pixels, resulting in the smoothest output, but is significantly slower.
The choice depends on the desired balance between processing time and visual fidelity.

OUTPUT IMAGE DIMENSIONS

By default, when rotating by multiples of 90 degrees, pnmrotate attempts to maintain the original image dimensions where sensible. For arbitrary angle rotations, the output image size will be a bounding box that perfectly encloses the rotated image, which will typically be larger than the original image dimensions.
Using the -exact option forces the output dimensions to be precisely the minimal size to contain the rotated image, which can further increase the image's dimensions if the rotated image's bounding box is larger. This ensures no cropping occurs but may add more background space.

HISTORY

pnmrotate is a core component of the Netpbm package, a suite of graphics utilities that originated from Jef Poskanzer's PBMplus toolkit in the late 1980s. Netpbm maintains a Unix philosophy of small, specialized tools that can be chained together. pnmrotate was developed to complement simpler rotation tools like pnmflip (which handles only 90-degree increment rotations), providing the capability for precise, arbitrary angle rotations. Its long-standing presence in the Netpbm suite underscores its utility and the stability of the PNM format for command-line image manipulation.

SEE ALSO

pnmflip(1), pnmscale(1), pnmgamma(1), netpbm(1)

Copied to clipboard