composite draws another image on top of this one at a position, respecting both images’ transparency.
Use it for watermarks, stickers, collages and anything else built from layers. A blend mode controls how the colors combine, and two opacity options fade either layer.
Signature
image.composite( src: Jimp, x?: number, y?: number, options?: { mode?: BlendMode; opacitySource?: number; opacityDest?: number; },): thiscomposite changes this image in place and returns it, so you can chain more calls after it. The source image isn’t changed.
It’s built into @jimp/core, so every Jimp class has it, including custom ones.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
src |
Jimp |
required | The image to draw on top. Any Jimp image, from any Jimp class. |
x |
number |
0 |
Where the left edge of src goes, in pixels. Can be negative or fractional (it’s rounded). |
y |
number |
0 |
Where the top edge of src goes. |
options |
object |
{} |
See Options. |
Options
| Option | Type | Default | Description |
|---|---|---|---|
mode |
BlendMode |
BlendMode.SRC_OVER |
How the source’s colors combine with the image’s. See blend modes. |
opacitySource |
number |
1 |
Multiplies the source’s alpha, from 0 (invisible) to 1. Clamped to that range. |
opacityDest |
number |
1 |
Multiplies the alpha of every pixel of this image before drawing, from 0 to 1. Clamped. |
BlendMode is a named export of jimp:
| Constant | Effect |
|---|---|
BlendMode.SRC_OVER |
The source is painted over the image. The default. |
BlendMode.DST_OVER |
The source goes under the image and shows only where it’s transparent. |
BlendMode.MULTIPLY |
Multiplies colors. Only darkens; white has no effect. |
BlendMode.ADD |
Adds colors. Only brightens, and clips to white quickly. |
BlendMode.SCREEN |
Inverse of multiply. Only lightens; black has no effect. |
BlendMode.OVERLAY |
Multiply in the image’s dark areas, screen in its light areas. |
BlendMode.DARKEN |
Keeps the darker value of each channel. |
BlendMode.LIGHTEN |
Keeps the lighter value of each channel. |
BlendMode.HARD_LIGHT |
Like overlay, but decided by the source’s brightness instead. |
BlendMode.DIFFERENCE |
The difference of each channel. White inverts, black has no effect. |
BlendMode.EXCLUSION |
A softer, lower-contrast difference. |
BlendMode.COLOR_DODGE |
Brightens the image to reflect the source. Black has no effect. |
BlendMode.COLOR_BURN |
Darkens the image to reflect the source. White has no effect. |
BlendMode.SOFT_LIGHT |
A gentler hard light: light source colors dodge, dark ones burn. |
BlendMode.HUE |
The source’s hue, with the image’s saturation and luminosity. |
BlendMode.SATURATION |
The source’s saturation, with the image’s hue and luminosity. |
BlendMode.COLOR |
The source’s hue and saturation, with the image’s luminosity. |
BlendMode.LUMINOSITY |
The source’s luminosity, with the image’s hue and saturation. |
The blend modes follow the W3C Compositing and Blending spec, so they give the same colors as CSS mix-blend-mode and canvas globalCompositeOperation.
The Compositing page shows every mode side by side.
Example
Add a watermark: shrink a logo, then draw it at half opacity in the bottom-right corner.
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");const logo = await Jimp.read("logo.png");
logo.resize({ w: Math.round(image.width / 5) });
image.composite( logo, image.width - logo.width - 20, image.height - logo.height - 20, { opacitySource: 0.5 },);
await image.write("photo-watermarked.jpg");Use a blend mode
Pass mode to change how colors combine. MULTIPLY is a good way to lay a texture or a dark design over a photo, since white parts of the source disappear:
import { BlendMode, Jimp } from "jimp";
const photo = await Jimp.read("wall.jpg");const stencil = await Jimp.read("stencil.png");
photo.composite(stencil, 0, 0, { mode: BlendMode.MULTIPLY });COLOR takes its colors from the source and keeps the image’s light and shade, so a solid color over a black and white photo tints it like an old print:
import { BlendMode, Jimp } from "jimp";
const photo = await Jimp.read("portrait.jpg");const tint = new Jimp({ width: photo.width, height: photo.height, color: 0x2b6cb0ff });
photo.greyscale().composite(tint, 0, 0, { mode: BlendMode.COLOR });Build an image from scratch
Start from a blank canvas with new Jimp() and composite layers onto it:
const canvas = new Jimp({ width: 800, height: 400, color: 0xffffffff });
canvas .composite(left, 0, 0) .composite(right, 400, 0) .composite(badge, 360, 160);Try it
Click the image to move the sticker, choose a mode and fade either layer with the opacity sliders.
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");
Notes
- Out-of-bounds parts are skipped. Pixels of
srcthat land outside the image are ignored, so a negative or largex,ycrops the source at the edges. The image never grows. - Transparency. In every mode the result’s alpha is
src + dst - src × dst, the standard “over” formula, so fully transparent source pixels leave the image unchanged. - Any Jimp class.
srccan come from a different Jimp class than the image, such as a custom Jimp and thejimppackage’sJimp. Anything that isn’t an image throwsThe source must be a Jimp image: an object with a bitmap of width, height and data. - Color profiles. If
srcis in a different color space from the image, for example an sRGB logo on a Display P3 photo, a copy of it is converted to the image’s color space first, so its colors look the same. See Color spaces. - Opacity values are clamped.
opacitySourceandopacityDestare clamped to0–1. A value that isn’t a number is treated as1. - Unknown modes. A
modethat isn’t aBlendModevalue throwsUnknown blend mode "nope", expected one of: srcOver, dstOver, multiply, add, screen, overlay, darken, lighten, hardLight, difference, exclusion, colorDodge, colorBurn, softLight, hue, saturation, color, luminosity(with your value in the quotes). - Non-number positions throw
x and y must be numbers. compositeorblit?blitcan copy just part of the source image, and always blends likeSRC_OVERat full opacity.- Changed in 2.0:
srchad to come from the same Jimp class as the image, orcompositethrewThe source must be a Jimp image. An unknownmodethrewTypeError: blendmode is not a function, and an opacity outside0–1was treated as1, so a negative opacity drew the source at full strength. Sources in a different color space were drawn without conversion. Results were truncated instead of rounded, so an opaque source over a semi-transparent pixel could give alpha 254.