Skip to content
docs
GitHub 4.5M /week v2.2.0
Size & shape

autocrop()

Remove borders of the same color from the edges.

From @jimp/plugin-crop

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

autocrop.d.ts
image.autocrop(options?: {
tolerance?: number;
cropOnlyFrames?: boolean;
cropSymmetric?: boolean;
leaveBorder?: number;
ignoreSides?: { north?: boolean; south?: boolean; east?: boolean; west?: boolean };
}): this
image.autocrop(tolerance: number): this

autocrop 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

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("logo-with-padding.png");
image.autocrop();
await image.write("logo.png");

Keep some padding

example.ts
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

example.ts
// Trim the bottom and sides of a screenshot, but keep the top
image.autocrop({ ignoreSides: { north: true } });

How it works

  1. The color of the pixel at (0, 0) is the border color.
  2. Starting from each edge, autocrop counts the rows (top and bottom) and columns (left and right) whose pixels are all within tolerance of that color. It scans the top first, then the left, bottom and right.
  3. With cropOnlyFrames: true, the image is cropped only if every side that isn’t in ignoreSides has a count above 0. This is checked before leaveBorder is applied.
  4. leaveBorder is subtracted from each count, then cropSymmetric evens out opposite sides. Negative counts become 0.

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.

Try autocrop()
image.autocrop()

Trim the border that matches the top-left pixel. The dice image has a transparent border to trim.

0
0px
cropOnlyFrames
cropSymmetric
Loading Jimp…Drop an image to load it
PNG
index.js
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 0x00000000 and 0xff000000 are both part of a transparent border.
  • Measure without cropping. autocropRect takes the same options and returns the rectangle instead of cropping.
  • Validation. tolerance must be between 0 and 1 when passed in the options object; a larger value fails with Too big: expected number to be <=1. A bare number isn’t checked. Unknown keys in ignoreSides, such as top, are ignored.
  • Changed in 2.0: ignoreSides and a large leaveBorder work with the default cropOnlyFrames: true, and fully transparent pixels match each other. Before, ignoreSides never cropped anything unless cropOnlyFrames was false, a leaveBorder as wide as any side’s border cancelled the whole crop, and transparent pixels hiding different colors stopped the trim.