Skip to content
docs
GitHub 4.5M /week v2.2.0
Blur & effects

close()

Fill small dark holes and gaps while keeping larger shapes the same size.

From @jimp/plugin-morphology

close dilates the image and then erodes it with the same options. Dark holes, gaps and specks smaller than the neighborhood are filled in, while larger shapes keep their size. Use it to remove dark specks from a scanned page or to fill holes in a mask.

Signature

close.d.ts
image.close(options: { radius: number; shape?: "square" | "disk" }): this

close changes the image in place and returns it, so you can chain more calls after it. It’s the same as image.dilate(options).erode(options). The options type is exported from jimp as MorphologyOptions.

Options

Option Type Default Description
radius number required How far the neighborhood reaches from each pixel, in pixels. It’s rounded to a whole number, and 0 or less does nothing.
shape "square" | "disk" "square" "square" is the (2 × radius + 1) pixel square around each pixel. "disk" is the pixels within radius of it, which gives rounder results.

Example

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("photo.png");
image.close({ radius: 2 });
await image.write("photo-closed.png");

Clean up a scanned document

Scans and photos of paper pick up dust, grain and an uneven background. threshold with max: "otsu" turns the paper pure white, and close then removes the dark specks that are left:

example.ts
import { Jimp } from "jimp";
const scan = await Jimp.read("scan.png");
scan.threshold({ max: "otsu" }).close({ radius: 1 });
await scan.write("scan-clean.png");

close also removes dark lines narrower than 2 × radius + 1 pixels, so keep the radius smaller than the thinnest stroke you want to keep. A radius of 1 removes specks up to 2 pixels across. To make the remaining text heavier, follow with erode.

Fill holes in a mask

This builds a black-and-white mask from a photo on a white background, then closes it. Light details inside the shape, such as highlights or white print, are above the cut-off and leave holes in the mask; close fills them:

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("product.png"); // on a white background
const mask = image
.clone()
.threshold({ max: 230 }) // background: 255
.invert() // background: 0, shape: 26 or more
.threshold({ max: 1 }) // shape: 255
.close({ radius: 12, shape: "disk" }); // fill the holes
image.mask(mask);
await image.write("product-cut-out.png");

The first threshold makes the near-white background pure white and leaves the shape’s gray levels, invert makes the background black, and the second threshold turns everything that isn’t background white. A radius of 12 fills holes up to about 25 pixels across.

If the mask also has white specks outside the shape, run open on it as well.

Try it

Drag radius and switch shape. On the dice, the dark gaps between nearby pips fill in, so the pips join up as the radius grows, while the dice keep their size.

Try close()
image.close({ radius: 4, shape: "disk" })

Dilate, then erode. Fills dark holes and gaps smaller than the radius and keeps the size of larger shapes.

4px
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

  • Shapes keep their size. The dilation and erosion cancel out everywhere except where a hole or gap was filled. With "disk", sharp dark corners get rounded.
  • Light and dark. close fills dark holes. On dark text or line art on a light background, that means it removes dark specks and thin dark lines. To fill light gaps in dark strokes, use open.
  • Each channel on its own. Red, green, blue and alpha are each processed separately. On a grayscale or black-and-white image this is the usual closing. On a color image, a pixel can end up with channels taken from different neighbors.
  • Transparency. Alpha is closed too, so on a transparent background small transparent holes and gaps inside opaque shapes are filled.
  • Edges don’t wrap. The neighborhood is clipped to the image: pixels near an edge only look at the pixels inside it. Pixels outside the image don’t count as black or white, and nothing wraps around from the other side.
  • Radius. radius is rounded to the nearest whole number, so 1.5 is 2 and 0.4 is 0. A radius of 0 or less leaves the image unchanged, without an error. Radii larger than the image’s width plus height are capped, since that already covers the whole image.
  • Speed. close makes two passes. A "square" costs the same for any radius. A "disk" gets slower as the radius grows, so prefer a square for large radii.
  • Errors. Calling close() with no options, a radius that isn’t a number or a shape other than "square" or "disk" throws a ZodError.
  • Added in 2.0.