pnmcrop
Crop images in PNM format
TLDR
Remove white borders on a PNM image
Remove borders of the specified color that are on the top and left side of the image
Determine the color of the borders to be removed by the color of the pixel in the specified corner
Leave a border with a width of n pixels. Additionally, specify the behaviour if the image is entirely made out of background
SYNOPSIS
pnmcrop [ options ] [ PNMFILE ]
PARAMETERS
-white
Assumes the border color is white for automatic cropping.
-black
Assumes the border color is black for automatic cropping.
-top
Crops only from the top edge of the image.
-bottom
Crops only from the bottom edge of the image.
-left
Crops only from the left edge of the image.
-right
Crops only from the right edge of the image.
-sides
Crops from both the left and right edges.
-margins
Crops from all four margins (top, bottom, left, and right).
-margin N
Crops a fixed N pixels from all four margins of the image.
-margin-top N
Crops a fixed N pixels from the top margin.
-margin-bottom N
Crops a fixed N pixels from the bottom margin.
-margin-left N
Crops a fixed N pixels from the left margin.
-margin-right N
Crops a fixed N pixels from the right margin.
DESCRIPTION
pnmcrop is a fundamental utility within the Netpbm suite, designed to crop PNM (Portable Anymap) images. Its primary function is to remove uniform-colored borders automatically. By default, it intelligently detects the border color by analyzing the four corner pixels of the image. If these corners do not exhibit a consistent color, pnmcrop defaults to assuming the border color is white. Users have granular control, being able to explicitly specify the border color (either -white or -black) or direct the cropping operation to specific sides only, such as -top, -bottom, -left, -right, or all four -margins. Beyond automatic border detection, pnmcrop also supports cropping a precise, fixed number of pixels from any or all edges, offering exact control over the output image dimensions. This command adheres to the Unix philosophy, reading image data from standard input (or a specified file) and writing the resulting cropped image to standard output, making it ideal for integration into shell pipelines.
CAVEATS
The automatic border detection relies on the color of the four corner pixels. If the border is non-uniform, transparent, or its color doesn't match the corners, the cropping result might not be as expected. pnmcrop is designed primarily for uniform-colored borders and works exclusively with PNM format images.
INPUT AND OUTPUT
pnmcrop reads the image data from a specified PNMFILE argument. If no file is provided, it defaults to reading from standard input. The resulting cropped image is always written to standard output. This design makes it highly versatile and efficient for use within shell scripts and pipelines, allowing it to easily chain with other Netpbm utilities or standard Unix commands.
DEFAULT CROPPING LOGIC
When no specific cropping options are provided, pnmcrop employs an intelligent default behavior. It attempts to automatically detect and crop away a uniform-colored border. To determine this border color, it samples the colors of the image's four corner pixels. If these sampled corner colors are not identical, indicating a non-uniform or ambiguous border, pnmcrop falls back to assuming the border is white, and then proceeds to crop based on this assumption.
HISTORY
pnmcrop is an integral part of the Netpbm project, a comprehensive suite of utilities for converting and manipulating graphic files. The Netpbm toolkit evolved from Jef Poskanzer's original PBM (Portable BitMap) utilities, which began in the late 1980s. Over time, it expanded to include PGM (Portable GreyMap) and PPM (Portable PixMap), collectively known as PNM. Bryan Henderson is credited as the author for pnmcrop. Netpbm tools are renowned for their adherence to the Unix philosophy – doing one thing well – and their long-standing presence and utility in the Linux/Unix ecosystem, often used in command-line pipelines for image processing.