autocrop trims plain borders from the edges of the image, such as the white margin around a scan or the transparent space around a logo.
The border color is the color of the top-left pixel.
Signature
image.autocrop(options?: { tolerance?: number; cropOnlyFrames?: boolean; cropSymmetric?: boolean; leaveBorder?: number; ignoreSides?: { north?: boolean; south?: boolean; east?: boolean; west?: boolean };}): thisimage.autocrop(tolerance: number): thisautocrop changes the image in place and returns it, so you can chain more calls after it. If there is nothing to trim, the image is left as it is.
Options
| Option | Type | Default | Description |
|---|---|---|---|
tolerance |
number |
0.0002 |
How different from the top-left pixel a pixel can be and still count as border, from 0 (exact match) to 1. See Tolerance. |
cropOnlyFrames |
boolean |
true |
Only crop if every side that isn’t in ignoreSides has a border. When false, any side with a border is trimmed. |
cropSymmetric |
boolean |
false |
Trim the same amount from opposite sides: the smaller of left and right, and the smaller of top and bottom. |
leaveBorder |
number |
0 |
Pixels of border to keep on every side. |
ignoreSides |
object |
none | Sides to leave alone: { north, south, east, west }, where north is the top. |
Passing a number, as in autocrop(0.01), is the same as autocrop({ tolerance: 0.01 }).
Example
import { Jimp } from "jimp";
const image = await Jimp.read("logo-with-padding.png");
image.autocrop();
await image.write("logo.png");Keep some padding
image.autocrop({ leaveBorder: 10 });leaveBorder is taken off what would be trimmed on each side. A side whose border is 10 pixels or thinner is left as it is, and the other sides are still trimmed.
Trim only some sides
// Trim the bottom and sides of a screenshot, but keep the topimage.autocrop({ ignoreSides: { north: true } });How it works
- The color of the pixel at
(0, 0)is the border color. - Starting from each edge,
autocropcounts the rows (top and bottom) and columns (left and right) whose pixels are all withintoleranceof that color. It scans the top first, then the left, bottom and right. - With
cropOnlyFrames: true, the image is cropped only if every side that isn’t inignoreSideshas a count above0. This is checked beforeleaveBorderis applied. leaveBorderis subtracted from each count, thencropSymmetricevens out opposite sides. Negative counts become0.
At least one row and one column are always left, so a single-color image is never cropped away completely.
Tolerance
tolerance is compared against colorDiff, which returns the squared distance between two colors scaled to the range 0 to 1.
The default 0.0002 only allows a difference of about 3 levels (out of 255) in each channel, so JPEG noise or a soft shadow can stop the scan early.
Raise it when a border isn’t fully removed:
tolerance |
Allows roughly |
|---|---|
0.0002 |
3 levels per channel. Clean, lossless borders. |
0.002 |
11 levels per channel. JPEG artifacts. |
0.01 |
25 levels per channel. Uneven scans and paper. |
Try it
The dice image has a transparent border to trim. Raise leaveBorder to keep some of it, and turn on cropSymmetric to trim the same amount from opposite sides.
image.autocrop()Trim the border that matches the top-left pixel. The dice image has a transparent border to trim.
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
Notes
- Different border colors. Only the top-left pixel’s color is trimmed. If the corners or sides differ, crop with an explicit rectangle using
crop. - Transparent borders. Fully transparent pixels all count as the same color, whatever color they hide, so
0x00000000and0xff000000are both part of a transparent border. - Measure without cropping.
autocropRecttakes the same options and returns the rectangle instead of cropping. - Validation.
tolerancemust be between0and1when passed in the options object; a larger value fails withToo big: expected number to be <=1. A bare number isn’t checked. Unknown keys inignoreSides, such astop, are ignored. - Changed in 2.0:
ignoreSidesand a largeleaveBorderwork with the defaultcropOnlyFrames: true, and fully transparent pixels match each other. Before,ignoreSidesnever cropped anything unlesscropOnlyFrameswasfalse, aleaveBorderas wide as any side’s border cancelled the whole crop, and transparent pixels hiding different colors stopped the trim.