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

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:

example.ts
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:

example.ts
// Centered
photo.composite(logo, (photo.width - logo.width) / 2, (photo.height - logo.height) / 2);
// Bottom-right corner, with a 20px margin
photo.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.

example.ts
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:

example.ts
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:

  • opacitySource fades the layer you’re drawing. 0.5 draws it at half strength, which is how you make a subtle watermark.
  • opacityDest multiplies 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.
example.ts
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:

example.ts
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:

A star sticker composited onto a photo of dice with BlendMode.SRC_OVER
SRC_OVER
The source over the image. The default.
A star sticker composited onto a photo of dice with BlendMode.DST_OVER
DST_OVER
The source under the image.
A star sticker composited onto a photo of dice with BlendMode.MULTIPLY
MULTIPLY
Darkens. White disappears.
A star sticker composited onto a photo of dice with BlendMode.ADD
ADD
Brightens, clipping to white.
A star sticker composited onto a photo of dice with BlendMode.SCREEN
SCREEN
Lightens. Black disappears.
A star sticker composited onto a photo of dice with BlendMode.OVERLAY
OVERLAY
Contrast, decided by the image.
A star sticker composited onto a photo of dice with BlendMode.DARKEN
DARKEN
The darker value per channel.
A star sticker composited onto a photo of dice with BlendMode.LIGHTEN
LIGHTEN
The lighter value per channel.
A star sticker composited onto a photo of dice with BlendMode.HARD_LIGHT
HARD_LIGHT
Contrast, decided by the source.
A star sticker composited onto a photo of dice with BlendMode.DIFFERENCE
DIFFERENCE
White inverts the image.
A star sticker composited onto a photo of dice with BlendMode.EXCLUSION
EXCLUSION
A softer difference.
A star sticker composited onto a photo of dice with BlendMode.COLOR_DODGE
COLOR_DODGE
Brightens. Black disappears.
A star sticker composited onto a photo of dice with BlendMode.COLOR_BURN
COLOR_BURN
Darkens. White disappears.
A star sticker composited onto a photo of dice with BlendMode.SOFT_LIGHT
SOFT_LIGHT
A gentle hard light.
A star sticker composited onto a photo of dice with BlendMode.HUE
HUE
The source's hue.
A star sticker composited onto a photo of dice with BlendMode.SATURATION
SATURATION
The source's saturation.
A star sticker composited onto a photo of dice with BlendMode.COLOR
COLOR
The source's hue and saturation.
A star sticker composited onto a photo of dice with BlendMode.LUMINOSITY
LUMINOSITY
The 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:

Try composite()
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.

sticker center
1
1
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");

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.

example.ts
const sheet = await Jimp.read("sprites.png");
const frame = new Jimp({ width: 32, height: 32 });
// The third 32 × 32 sprite in the top row
frame.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:

example.ts
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.

Try blit()
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.

source region
x, y
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");

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.

example.ts
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.

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");

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