posterize limits each color channel to n evenly spaced levels from 0 to 255, replacing smooth gradients with flat bands of color like a screen-printed poster.
Signature
image.posterize(n: number): thisposterize changes the image in place and returns it, so you can chain more calls after it.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
n |
number |
required | The number of levels per channel, from 2 to 256. Other values are rounded and clamped to that range. |
Example
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");
image.posterize(5);
await image.write("photo-poster.jpg");A bold graphic look
Very few levels give strong, flat shapes. Raising the saturation first makes the colors pop:
image.color([{ apply: "saturate", params: [40] }]).posterize(3);Try it
Drag the slider to change n.
image.posterize(4)Round each color channel to the nearest of n levels.
Loading Jimp…Drop an image to load it
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
- Levels per channel, not colors. Each of red, green and blue gets
nlevels, so the image can have up ton³colors:n = 4allows 64. To choose the exact number of colors, usequantize. - The levels. With
nlevels the values are0,255 / (n − 1),2 × 255 / (n − 1)…255, rounded. Forn = 4that’s0,85,170and255; forn = 2it’s0and255. - Nearest level. Each channel moves to the closest level, so the image keeps its overall brightness: at
n = 2, channels up to127become0and channels from128become255. - Range.
nis rounded to a whole number and clamped to2–256.posterize(256)leaves the image unchanged. - Alpha is unchanged.
- Errors. An
nthat isn’t a number throwsn must be numbers. - Changed in 2.0: each channel goes to the nearest level; before, it was rounded down to the level below, which darkened the image, most of all with few levels.