threshold()
Turn pixels at or above a cut-off into one gray value.
threshold converts the image to grayscale and then replaces every pixel at or above a cut-off with a single value, white by default.
Darker pixels keep their gray level. Use it to clean up scans, wash out light backgrounds or prepare an image for a mask.
Signature
image.threshold(options: { max: number | "otsu"; replace?: number; autoGreyscale?: boolean;}): thisthreshold changes the image in place and returns it, so you can chain more calls after it.
The options type is exported from jimp as ThresholdOptions.
Options
| Option | Type | Default | Description |
|---|---|---|---|
max |
number | "otsu" |
required | The cut-off, from 0 to 255. Values of max or more are replaced. "otsu" picks the cut-off from the image. |
replace |
number |
255 |
The value, from 0 to 255, that replaced values get. |
autoGreyscale |
boolean |
true |
Call greyscale first. With false, each color channel is thresholded on its own. |
Example
import { Jimp } from "jimp";
const image = await Jimp.read("scan.jpg");
// Everything at least as light as 200 becomes pure whiteimage.threshold({ max: 200 });
await image.write("scan-clean.png");Pick the cut-off automatically
max: "otsu" chooses the cut-off with Otsu’s method, which looks for the value that best splits the image into a dark and a light group.
It works well for scans and photos of documents, where the right cut-off changes from image to image.
image.threshold({ max: "otsu" });To see which cut-off was used, compute it with otsuThreshold and pass it as max. The result is the same:
import { Jimp, otsuThreshold } from "jimp";
const image = await Jimp.read("scan.jpg");const level = otsuThreshold(image);
console.log(level); // e.g. 104image.threshold({ max: level });Replace light pixels with black
Set replace to choose another value:
image.threshold({ max: 180, replace: 0 });Keep the colors
With autoGreyscale: false the image isn’t converted to gray.
Each of red, green and blue is compared with max and replaced on its own, so channels below the cut-off keep their color:
image.threshold({ max: 200, autoGreyscale: false });// (230, 120, 40) becomes (255, 120, 40)Pure black and white
threshold only changes the values at or above max; darker pixels keep their gray.
For a two-color result, set the remaining pixels yourself with scan:
image.threshold({ max: "otsu" });
image.scan((x, y, idx) => { const value = image.bitmap.data[idx] === 255 ? 255 : 0;
image.bitmap.data[idx] = value; image.bitmap.data[idx + 1] = value; image.bitmap.data[idx + 2] = value;});For a black and white image that keeps shading with patterns of dots, use dither instead.
Clean up the result
A thresholded scan or mask often has specks and small holes. close removes dark specks and fills dark holes, and open removes light specks:
image.threshold({ max: "otsu" }).close({ radius: 1 });See Clean up a scanned document and Fill holes in a mask.
Try it
Drag max to move the cut-off and replace to change the value that light pixels get, or turn on otsu to let Jimp pick the cut-off.
image.threshold({ max: 140 })Convert to greyscale, then set every pixel at or above max to replace. Darker pixels keep their gray value.
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
Notes
- At or above. A value is replaced when it’s greater than or equal to
max.max: 0replaces every pixel;max: 255replaces only pure white. - Otsu with
autoGreyscale: false."otsu"reads the image’s values after the optionalgreyscale. WithautoGreyscale: falseit builds its histogram from the red channel alone, so use it that way only on images that are already gray. - Alpha is unchanged. Transparent pixels are processed like any other, but stay transparent. With
"otsu", transparent pixels don’t affect the chosen cut-off. - Errors.
optionsis required: callingthreshold()with no argument throws aZodError. So do a missingmax, a string other than"otsu", and values ofmaxorreplaceoutside0–255. - Added in 2.0:
max: "otsu". - Changed in 2.0: with
autoGreyscale: false, each channel is compared and replaced on its own; before, every pixel came out gray, with its level taken from the red channel alone. - Changed in 2.0: the gray values from
greyscaleare rounded rather than rounded down, so some pixels come out one level lighter.