open()
Remove small bright specks while keeping larger shapes the same size.
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
image.open(options: { radius: number; shape?: "square" | "disk" }): thisopen 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
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:
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.
image.open({ radius: 4, shape: "disk" })Erode, then dilate. Removes bright specks smaller than the radius and keeps the size of larger bright shapes.
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.
radiusis rounded to the nearest whole number, so1.5is2and0.4is0. A radius of0or 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.
openmakes 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, aradiusthat isn’t a number or ashapeother than"square"or"disk"throws aZodError. - Added in 2.0.