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

open()

Remove small bright specks while keeping larger shapes the same size.

From @jimp/plugin-morphology

open erodes the image and then dilates it with the same options. Bright specks and thin bright lines smaller than the neighborhood disappear, while larger bright shapes keep their size. Use it to remove white noise from a mask or a thresholded image.

Signature

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

open changes the image in place and returns it, so you can chain more calls after it. It’s the same as image.erode(options).dilate(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.open({ radius: 2 });
await image.write("photo-opened.png");

Remove specks from a mask

A mask made with threshold often has stray white pixels in its black area, where the photo’s background happened to be dark. Open the mask to remove them before you apply it. Anything narrower than 2 × radius + 1 pixels goes, so pick a radius just large enough for the specks:

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("photo.png");
const mask = await Jimp.read("mask.png"); // white where the shape is
mask.open({ radius: 2, shape: "disk" });
image.mask(mask);

To build the mask itself from a photo, see Fill holes in a mask, which uses close for the opposite problem. Masks often need both: open for specks outside the shape, then close for holes inside it.

Dark ink on a light page

For dark text or line art on a light background, the roles swap: open fills small light gaps in the strokes, such as breaks in a faded line. To remove dark specks from a scan, use close instead.

Try it

Drag radius and switch shape. On the dice, the white pips disappear once the radius is large enough, while the dice keep their size.

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

Erode, then dilate. Removes bright specks smaller than the radius and keeps the size of larger bright 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

  • Bright shapes keep their size. Shapes that survive the erosion grow back by the same amount. Thin bright parts that the neighborhood doesn’t fit inside are removed, and with "disk" sharp bright corners get rounded.
  • 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 opening. On a color image, a pixel can end up with channels taken from different neighbors.
  • Transparency. Alpha is opened too, so on a transparent background small or thin opaque bits disappear and larger opaque shapes keep their size.
  • 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. open 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 open() 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.