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
image.opacity(f: number): thisopacity 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
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:
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.
image.opacity(0.5)Multiply the alpha channel of every pixel by f, from 0 (transparent) to 1.
Loading Jimp…Drop an image to load it
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
opacitycan’t make an image more opaque. To make every pixel fully opaque, useopaque. - 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.
opacityorfade?fade(f)is the same asopacity(1 - f): it says how much to remove instead of how much to keep.- Errors. An
fthat isn’t a number throwsf must be a number. A value outside0to1throwsf must be a number from 0 to 1.