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

opacity()

Multiply the alpha channel by a factor from 0 to 1.

From @jimp/plugin-color

opacity makes the whole image more transparent by multiplying the alpha channel of every pixel by f. 1 leaves the image unchanged, 0.5 makes it half as opaque and 0 makes it fully transparent.

Signature

opacity.d.ts
image.opacity(f: number): this

opacity changes the image in place and returns it, so you can chain more calls after it.

Parameters

Parameter Type Default Description
f number required The factor to multiply each pixel’s alpha by, from 0 to 1.

Example

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("logo.png");
image.opacity(0.5);
await image.write("logo-half.png");

A translucent watermark

Lower the opacity of a logo, then draw it on a photo with composite:

example.ts
import { Jimp } from "jimp";
const photo = await Jimp.read("photo.jpg");
const logo = await Jimp.read("logo.png");
logo.resize({ w: 120 }).opacity(0.4);
photo.composite(logo, photo.width - logo.width - 16, photo.height - logo.height - 16);
await photo.write("photo-watermarked.jpg");

composite also takes an opacitySource option, which does the same without changing logo.

Try it

Drag the slider to change f. Transparent areas show the background through them.

Try opacity()
image.opacity(0.5)

Multiply the alpha channel of every pixel by f, from 0 (transparent) to 1.

0.5
Loading Jimp…Drop an image to load it
PNG
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

  • Multiplies, never adds. Pixels that are already transparent stay transparent, and opacity can’t make an image more opaque. To make every pixel fully opaque, use opaque.
  • Colors are unchanged. Only alpha changes. Saving to a format without transparency, such as JPEG, drops the alpha channel again, so the image looks unchanged there.
  • opacity or fade? fade(f) is the same as opacity(1 - f): it says how much to remove instead of how much to keep.
  • Errors. An f that isn’t a number throws f must be a number. A value outside 0 to 1 throws f must be a number from 0 to 1.