Compositing
Layer images on top of each other with composite, blit and mask, and control how they blend.
Compositing means building one image out of several: a logo on a photo, a row of thumbnails on a canvas, a picture cut to a shape.
Jimp has three methods for it. composite layers a whole image with blend modes and opacity, blit copies an image or a rectangle of it, and mask uses an image’s brightness to make parts of another transparent.
Layering one image on another
Every compositing call is made on the image that receives the pixels, the destination. The image you pass in is the source, and it isn’t changed.
composite takes the source and the position of its top-left corner:
import { Jimp } from "jimp";
const photo = await Jimp.read("photo.jpg");const logo = await Jimp.read("logo.png");
photo.composite(logo, 20, 20);
await photo.write("photo-logo.jpg");Transparency is respected: transparent pixels of the logo let the photo show through, and partly transparent ones mix with it. The destination never grows. Any part of the source that lands outside it, for example because of a negative position, is cut off.
Positioning
Positions are in pixels from the destination’s top-left corner, and fractional values are rounded. Use the images’ sizes to place a layer relative to the edges or the center:
// Centeredphoto.composite(logo, (photo.width - logo.width) / 2, (photo.height - logo.height) / 2);
// Bottom-right corner, with a 20px marginphoto.composite(logo, photo.width - logo.width - 20, photo.height - logo.height - 20);Starting from a blank canvas
To build an image from parts, create an empty one with new Jimp() and composite onto it. color fills the canvas; leave it out for a transparent one.
import { Jimp } from "jimp";
const files = ["a.jpg", "b.jpg", "c.jpg"];const size = 200;const gap = 10;
const sheet = new Jimp({ width: files.length * size + (files.length + 1) * gap, height: size + gap * 2, color: 0xffffffff,});
for (const [i, file] of files.entries()) { const thumb = await Jimp.read(file); thumb.cover({ w: size, h: size }); sheet.composite(thumb, gap + i * (size + gap), gap);}
await sheet.write("contact-sheet.jpg");color takes an RGBA number such as 0xffffffff (opaque white) or a CSS color string such as "#1b1840". See Colors and pixels for the formats.
Because every compositing method returns the image, you can chain layers:
canvas .composite(background, 0, 0) .composite(photo, 40, 40) .composite(caption, 40, 360);Sources can come from any Jimp class, so images read with a custom Jimp and with the jimp package’s Jimp can be layered onto each other.
Opacity
composite’s fourth argument takes two opacity options, each from 0 to 1:
opacitySourcefades the layer you’re drawing.0.5draws it at half strength, which is how you make a subtle watermark.opacityDestmultiplies the alpha of every pixel of the destination before drawing, not only the pixels under the source. Use it when you want the whole background faded.
photo.composite(logo, 20, 20, { opacitySource: 0.4 });Values outside 0–1 are clamped to that range, and a value that isn’t a number counts as 1.
To fade an image without compositing it, use opacity or fade.
Blend modes
mode decides how each source color combines with the color under it. The modes are the BlendMode constants, a named export of jimp:
import { BlendMode, Jimp } from "jimp";
photo.composite(texture, 0, 0, { mode: BlendMode.MULTIPLY });Here is the same star sticker composited onto the same photo with every mode:

SRC_OVERThe source over the image. The default.

DST_OVERThe source under the image.

MULTIPLYDarkens. White disappears.

ADDBrightens, clipping to white.

SCREENLightens. Black disappears.

OVERLAYContrast, decided by the image.

DARKENThe darker value per channel.

LIGHTENThe lighter value per channel.

HARD_LIGHTContrast, decided by the source.

DIFFERENCEWhite inverts the image.

EXCLUSIONA softer difference.

COLOR_DODGEBrightens. Black disappears.

COLOR_BURNDarkens. White disappears.

SOFT_LIGHTA gentle hard light.

HUEThe source's hue.

SATURATIONThe source's saturation.

COLORThe source's hue and saturation.

LUMINOSITYThe source's brightness.
| Mode | What it does |
|---|---|
SRC_OVER |
Paints the source over the image. The default, and what you usually want. |
DST_OVER |
Puts the source under the image, so it only shows where the image is transparent. Handy for adding a background. |
MULTIPLY |
Multiplies the colors. The result is never lighter; white in the source has no effect. |
ADD |
Adds the colors. The result is never darker and clips to white quickly. |
SCREEN |
Inverts both, multiplies, and inverts back. The result is never darker; black in the source has no effect. |
OVERLAY |
Multiplies where the image is dark and screens where it’s light, which boosts contrast. |
DARKEN |
Keeps the darker of the two values in each channel. |
LIGHTEN |
Keeps the lighter of the two values in each channel. |
HARD_LIGHT |
Like OVERLAY, but chooses multiply or screen by the source’s brightness instead of the image’s. |
DIFFERENCE |
Subtracts the smaller value from the larger. White inverts the image; black has no effect. |
EXCLUSION |
Like DIFFERENCE with less contrast: mid-tones turn gray instead of saturated. |
COLOR_DODGE |
Divides the image by the inverse of the source. The result is never darker; brighter sources brighten more, and black has no effect. |
COLOR_BURN |
Divides the inverse of the image by the source and inverts it. The result is never lighter; darker sources darken more, and white has no effect. |
SOFT_LIGHT |
A gentler HARD_LIGHT: source colors lighter than 50% gray dodge the image, darker ones burn it. |
HUE |
The hue of the source, with the saturation and luminosity of the image. |
SATURATION |
The saturation of the source, with the hue and luminosity of the image. A gray source makes the image gray. |
COLOR |
The hue and saturation of the source, with the luminosity of the image. Tints an image and keeps its shading. |
LUMINOSITY |
The luminosity of the source, with the hue and saturation of the image. The opposite of COLOR. |
The modes from MULTIPLY on are the blend modes of the W3C Compositing and Blending spec, the same as CSS mix-blend-mode and canvas globalCompositeOperation, and they give the same colors. HUE, SATURATION, COLOR and LUMINOSITY mix the three channels together; the others work on each channel separately.
All modes compute the result’s alpha the same way, as src + dst - src × dst. Where the destination is fully transparent, every mode shows the source unchanged, so blend modes are only visible over opaque or partly transparent images.
Try them on your own images. Click to move the sticker and pick a mode:
image.composite(await Jimp.read("sticker.png"), 120, 75)Draw sticker.png on top with a blend mode and opacity. Click the image to move the sticker.
import { Jimp } from "jimp"; const image = await Jimp.read("dice-on-paper.png"); // Change an option to run it await image.write("output.png");
Copying regions with blit
blit copies pixels from a source onto the image, like composite with SRC_OVER at full opacity. What it adds is a source rectangle: srcX, srcY, srcW and srcH pick the part of the source to copy, and x, y say where it goes.
const sheet = await Jimp.read("sprites.png");const frame = new Jimp({ width: 32, height: 32 });
// The third 32 × 32 sprite in the top rowframe.blit({ src: sheet, srcX: 64, srcY: 0, srcW: 32, srcH: 32 });To move part of an image within itself, blit from a clone so the copy doesn’t read pixels it has already overwritten:
image.blit({ src: image.clone(), x: 10, y: 10, srcX: 120, srcY: 80, srcW: 100, srcH: 60 });The source rectangle is clamped to the source image, so a rectangle that runs past its edges copies only the part inside it.
image.blit({ src: image.clone(), x: 14, y: 14, srcX: 126, srcY: 95, srcW: 108, srcH: 81 })Copy pixels onto the image at x, y. Copy a region of the image itself (drag to pick it, click to place it), or sticker.png.
import { Jimp } from "jimp"; const image = await Jimp.read("dice-on-paper.png"); // Change an option to run it await image.write("output.png");
Cutting shapes with mask
mask uses a second image as a stencil. For every pixel it covers, the mask’s gray value scales the image’s alpha: white keeps the pixel, black makes it transparent and gray makes it partly transparent. The mask’s own alpha counts too, so transparent parts of a mask hide the image, and colored pixels count by their luminance.
const photo = await Jimp.read("photo.png");const shape = await Jimp.read("heart-mask.png"); // white heart on black
photo.mask(shape);
await photo.write("photo-heart.png");Masks only lower alpha, and only where they cover the image, so make them at least as large as the image. Pass { src, x, y } to position one. Save masked images as PNG or another format with an alpha channel.
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.
import { Jimp } from "jimp"; const image = await Jimp.read("dice-on-paper.png"); // Change an option to run it await image.write("output.png");
For circles and rounded rectangles you don’t need a mask image: use circle and roundCorners.
Color profiles
Most images are sRGB, but photos from phones are often Display P3. When a source’s color space differs from the destination’s, composite and blit convert a copy of the source to the destination’s space first, so an sRGB logo keeps its colors on a P3 photo. The source itself isn’t changed. mask only reads gray values, so it doesn’t convert. See Color spaces for more.
Which one to use
| You want to… | Use |
|---|---|
| Draw a whole image on top, possibly faded or blended | composite |
| Copy a rectangle out of a sprite sheet or tile set | blit with srcX, srcY, srcW, srcH |
| Put something behind an image’s transparent areas | composite with BlendMode.DST_OVER |
| Make parts of an image transparent with a stencil | mask |
| Add a drop shadow | boxShadow or shadow |
See also
composite,blitandmask: the full reference for each methodnew Jimp(): create blank canvases- Colors and pixels: color numbers, alpha and reading pixels
- Color spaces: Display P3, ICC profiles and converting between them
- Constants:
BlendModeand the other named exports boxShadow,opacityandcrop: methods that pair well with compositing