Skip to content
docs
GitHub 4.5M /week v2.2.0
Compositing

mask()

Use another image's brightness to make parts of this image transparent.

From @jimp/plugin-mask

mask uses a second, usually grayscale, image as a stencil: where the mask is white the image stays as it is, where it’s black the image becomes transparent, and grays in between make it partly transparent. Use it to cut an image into a shape, fade its edges into a vignette or reveal it through a gradient.

Signature

mask.d.ts
image.mask(src: Jimp): this
image.mask(options: { src: Jimp; x?: number; y?: number }): this

mask changes this image in place and returns it, so you can chain more calls after it. The mask image isn’t changed.

Options

Pass the mask on its own to line it up with the top-left corner, or an object to position it.

Option Type Default Description
src Jimp required The mask image. Its brightness sets how much of the image stays.
x number 0 Where the mask’s left edge goes on the image. Can be negative.
y number 0 Where the mask’s top edge goes.

Example

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("photo.png");
const mask = await Jimp.read("mask.png");
image.mask(mask);
await image.write("photo-masked.png");

Save the result as PNG (or another format with alpha) to keep the transparency.

Fade an image out

A mask doesn’t have to be a file. Here a mask the size of the image goes from white at the top to black at the bottom, so the image fades out toward its bottom edge:

example.ts
const fade = new Jimp({ width: image.width, height: image.height });
fade.scan((x, y, idx) => {
const v = Math.round(255 * (1 - y / (fade.height - 1)));
fade.bitmap.data[idx] = v;
fade.bitmap.data[idx + 1] = v;
fade.bitmap.data[idx + 2] = v;
fade.bitmap.data[idx + 3] = 255;
});
image.mask(fade);

Position the mask

Give x and y to move the mask. Here a spotlight mask twice the size of the image is centered on a point, so the image stays covered wherever the spot goes:

example.ts
const spotlight = await Jimp.read("spotlight.png"); // 720 × 540
const center = { x: 120, y: 90 };
image.mask({
src: spotlight,
x: center.x - spotlight.width / 2,
y: center.y - spotlight.height / 2,
});

Try it

Click the image to move the mask. Switch the mask to sticker.png, a colored star on a transparent background, to see a mask’s alpha and colors at work.

Try mask()
image.mask({ src: await Jimp.read("spotlight.png"), x: -180, y: -135 })

Use a mask's gray values and alpha to set the image's alpha: white keeps a pixel, black or transparent hides it. Click to move the mask.

mask center
Loading Jimp…Drop an image to load it
PNG
index.js
import { Jimp } from "jimp"; const image = await Jimp.read("dice-on-paper.png"); // Change an option to run it await image.write("output.png");

Notes

  • How the mask is read. For every pixel the mask covers, Jimp multiplies the image’s alpha by the mask pixel’s gray value and by the mask pixel’s alpha, both out of 255, and rounds the result. White keeps the pixel, black hides it, and a gray of 128 halves its alpha.
  • The mask’s own alpha counts. A transparent mask pixel hides the image, as if the mask were flattened onto black. So a shape with a transparent background, such as a logo, works as a mask on its own: the image shows only where the shape is.
  • Colored masks use luminance. A gray pixel (equal red, green and blue) is used as it is. A colored pixel is turned into gray the way GIMP converts sRGB to grayscale: its linear-light luminance, encoded back to sRGB. Pure red becomes 130, green 220 and blue 70.
  • Gray values aren’t linearized. GIMP stores layer masks as linear light, so a mask that looks 50% gray in GIMP keeps about 22% of the layer, while Jimp keeps 50%. To match GIMP, convert the mask’s gray values from sRGB to linear first.
  • Only the covered area changes. Pixels outside the mask’s area keep their alpha. Make the mask at least as large as the image, or resize it to { w: image.width, h: image.height }, to mask everything.
  • It only removes opacity. The image’s alpha is multiplied, never raised, so masking twice compounds and an already transparent pixel can’t be brought back.
  • Positions are rounded. Fractional x and y are rounded to whole pixels.
  • Validation. The options are checked with zod, so calling mask() without an image, or with an object without src, throws a validation error.
  • Changed in 2.0: colored mask pixels used to count by the average of red, green and blue (pure red was 85), the mask’s alpha was ignored (a transparent white pixel kept the image), and results were truncated instead of rounded. Opaque gray masks on opaque images give the same result as before.