Minimum unique abbreviation of option is acceptable. You may use double hyphens instead of single hyphen to denote options. You may use white space in place of the equals sign to separate an option name from its value.
This program is part of Netpbm.
pnmcrop reads a PBM, PGM, or PPM image as input, removes borders that are the background color, and produces the same type of image as output.
If you don't specify otherwise, pnmcrop assumes the background color is whatever color the top left and right corners of the image are and if they are different colors, something midway between them. You can specify that the background is white or black with the -white and -black options or make pnmcrop base its guess on all four corners instead of just two with -sides.
By default, pnmcrop chops off any stripe of background color it finds, on all four sides. You can tell pnmcrop to remove only specific borders with the -left, -right, -top, and -bottom options.
But note that pnmcrop's determination of the background color is independent of which edges you crop, which may not be intuitive. For example, imagine an image with a blue border at the top and a black border at the bottom and you say to crop the bottom (-bottom). You may have expected to crop the black border, but you actually won't crop anything, because pnmcrop considers the background color to be whatever color the top two corners are, which is blue, and there is no blue at the bottom of the image. If you do want pnmcrop to take the background color from the edges being cropped, use -bg-corner.
If you want to leave some border, use the -margin option. It will not only spare some of the border from cropping, but will fill in (with what pnmcrop considers the background color) if necessary to get up to that size.
If the input is a multi-image stream, pnmcrop processes each one independently and produces a multi-image stream as output. It chooses where to crop independently for each image. So if you start with a stream of images of the same dimensions, you may end up with images of differing dimensions. Before Netpbm 10.37 (December 2006), pnmcrop ignored all input images but the first.
If you want to chop a specific amount off the side of an image, use pamcut.
If you want to add different borders after removing the existing ones, use pamcat or pamcomp.
In addition to the options common to all programs based on libnetpbm (most notably -quiet, see Common Options), pnmcrop recognizes the following command line options:
You may specify at most one of -black, -white, -sides, -bg-color, and -bg-corner.
You may specify at most one of -black, -white, -sides, -bg-color, and -bg-corner.
You may specify at most one of -black, -white, -sides, -bg-color, and -bg-corner.
This option was new in Netpbm 10.86 (March 2019).
If at least three of the four corners are the same color, pnmcrop takes that as the background color. If not, pnmcrop looks for two corners of the same color in the following order, taking the first found as the background color: top, left, right, bottom. If all four corners are different colors, pnmcrop assumes an average of the four colors as the background color.
The -sides option slows pnmcrop down, as it reads the entire image to determine the background color in addition to the up to three times that it would read it without -sides.
You may specify at most one of -black, -white, -sides, -bg-color, and -bg-corner.
You may specify at most one of -black, -white, -sides, -bg-color, and -bg-corner.
This option was new in Netpbm 10.86 (March 2019).
This option was new in Netpbm 10.29 (August 2005).
You can use this if the image has borders that vary slightly in color, such as would be the case in a photograph. Consider a photograph against a white screen. The color of the screen varies slightly with shading and dirt and such, but is still quite distinct in color from the subject of the photograph. pnmcrop will choose some particular shade as the background color and if you specify an appropriate -closeness value, it will correctly identify all of the screen as background and crop it off.
To implement more complex rules for identifying background, use -borderfile.
The default is zero, which means a pixel's color must exactly match the background color for the pixel to be considered part of a background border.
This option was new in Netpbm 10.78 (March 2017). With older Netpbm, colors must match exactly.
Without this option, pnmcrop examines the input image and figures out what part of the image is border and what part is foreground (not border), as well as the background color. With this option, pnmcrop finds the borders in one image, then uses the those four border sizes (left, right, top, bottom) in cropping a different image. Furthermore, if you use -margin to add borders, the color of those borders is the background color pnmcrop detects in the border file.
The point of this is that you may want to help pnmcrop to come to a different conclusion as to where the borders are and what the background color is by preprocessing the input image. For example, consider an image that has speckles of noise in its borders. pnmcrop isn't smart enough to recognize these as noise; it sees them as foreground image. So pnmcrop considers most of your borders to be foreground and does not crop them off as you want. To fix this, run the image through a despeckler such as pbmclean and tell pnmcrop to use the despeckled version of the image as the -borderfile image, but the original speckled version as the input image. That way, you crop the borders, but retain the true foreground image, speckles and all.
The border file must have the same number of images in it as the input file; the background color determination for image N of the input is based on the image N of the border file.
This option was new in Netpbm 10.29 (August 2005).
Before Netpbm 10.46 (March 2009), the original image and not the border file determines the background color. pnmcrop fails if there is no apparent background color in the original image (i.e. the corners of the image don't have a common color).
This is a somewhat incongruous result; the mathematically consistent result of cropping the background from an image that is entirely background would be an image with no pixels at all. But such a thing does not exist in the Netpbm formats (and you probably wouldn't want it anyway, because whoever processes this output may not tolerate that).
The background can be more than one color when you specify -closeness, so it matters which row, column, or pixel remains. If you crop on the top and not bottom, it is the last row that remains. If you crop on both the top and bottom, it is the middle row that remains. The other cases follow similarly.
If you specify a margin (-margin), the output image consists entirely of the margins; there is no single row, column, or pixel between the margins. So with -margin, the incongruity mentioned above does not exist. But before Netpbm 10.92 (September 2020), -margin was ignored with -blank-image=minimize.
This is actually useful if you are trying to find a single set of cropping parameters to crop a stream of images. To do this, you could do a pass with -reportsize and -blank-image=maxcrop to compute the maximum crop for each edge, and then use those numbers in -cropxxx options on a pamcut pass to do the crop. In this scenario, any all-background (blank) images would have no effect on the cropping parameters you compute. If you do this, you must give special consideration to a stream with nothing but blank images.
-margin is always ignored when the image is all background.
This option was new in Netpbm 10.86 (March 2019).
0 +7 -20 -10 200 300 rgb-255:10/0/255 0.0
The line is composed of the following blank-delimited tokens:
You cannot use -borderfile together with this option.
This option was new in Netpbm 10.86 (March 2019).
You cannot use -borderfile together with this option.
This option was new in Netpbm 10.86 (March 2019).