Welcome to a tutorial on how to watermark images in NodeJS. So you need to add a watermark to images in NodeJS? In Javascript, we can use the Canvas API to do just that. Thankfully in NodeJS, we also have a Canvas library to emulate the native API – Read on for the examples!
ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
TLDR – QUICK SLIDES
Fullscreen Mode – Click Here
TABLE OF CONTENTS
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
QUICK NOTES
- Download and unzip into your project folder.
- Install Canvas –
npm install canvas
- Run the examples.
EXAMPLE CODE DOWNLOAD
Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
NODEJS WATERMARK IMAGES
All right, let us now get into the examples of adding a watermark to images in NodeJS.
1) SIMPLE TEXT WATERMARK
// (A) LOAD MODULES
const fs = require("fs"),
{ registerFont, createCanvas, loadImage } = require("canvas");
// (B) SETTINGS - CHANGE FONT TO YOUR OWN
var
sFile = "demo.png", // source image
sSave = "demoA.png", // "save as"
sText = "COPYRIGHT", // copywrite text
sX = 10, sY = 40; // text position
registerFont("C:/Windows/Fonts/arialbd.ttf", { family: "Arial Bold" });
// (C) LOAD IMAGE + DRAW TEXT
loadImage(sFile).then(img => {
// (C1) CREATE CANVAS
const canvas = createCanvas(img.width, img.height),
ctx = canvas.getContext("2d");
// (C2) DRAW IMAGE ONTO CANVAS
ctx.drawImage(img, 0, 0);
// (C3) WRITE COPYRIGHT TEXT ONTO IMAGE
ctx.font = '36px "Arial Bold"';
ctx.fillStyle = "rgba(255, 0, 0, 0.4)";
ctx.fillText(sText, sX, sY);
// (C4) SAVE
const out = fs.createWriteStream(sSave),
stream = canvas.createPNGStream();
stream.pipe(out);
out.on("finish", () => console.log("Done"));
});
A quick walkthrough for those who are lost:
- (C)
loadImage("SOURCE").then(...)
Make sure that the image is fully loaded before we add the watermark. - (C1)
createCanvas(WIDTH, HEIGHT)
Create an empty canvas. - (C2)
drawImage(IMAGE, X, Y)
Draw the source image onto the canvas. - (C3)
fillText(TEXT, X, Y)
Draw the text watermark onto the canvas. - (C4) Save the canvas to a file.
2) IMAGE WATERMARK
// (A) LOAD MODULES
const fs = require("fs"),
{ createCanvas, loadImage } = require("canvas");
// (B) SETTINGS
var
sFile = "demo.png", // source image
sMark = "potato.png", // watermark image
sSave = "demoB.png", // "save as"
sX = 10, sY = 10, // watermark position
ready = 0; // flag to track image load
// (C) ADD WATERMARK IMAGE
function mark () { if (ready==2) {
// (C1) CREATE CANVAS
const canvas = createCanvas(sFile.width, sFile.height),
ctx = canvas.getContext("2d");
// (C2) DRAW SOURCE IMAGE ONTO CANVAS
ctx.drawImage(sFile, 0, 0);
// (C3) DRAW WATERMARK IMAGE ONTO CANVAS
ctx.drawImage(sMark, sX, sY);
// (C4) SAVE
const out = fs.createWriteStream(sSave),
stream = canvas.createPNGStream();
stream.pipe(out);
out.on("finish", () => console.log("Done"));
}}
// (D) LOAD SOURCE + WATERMARK IMAGES
loadImage(sFile).then(img => {
sFile = img; ready++; mark();
});
loadImage(sMark).then(img => {
sMark = img; ready++; mark();
});
Keep calm and look carefully – This is pretty much the same as the text watermark. But we “copy” the source image onto the canvas, followed by “pasting” the image watermark on top.
P.S. For the uninitiated, make sure that the watermark image has a transparent background; Only PNG GIF WEBP
files support alpha channels (transparency).
3) CENTER IMAGE WATERMARK
// (A) LOAD MODULES
const fs = require("fs"),
{ createCanvas, loadImage } = require("canvas");
// (B) SETTINGS
var
sFile = "demo.png", // source image
sMark = "potato.png", // watermark image
sSave = "demoC.png", // "save as"
ready = 0; // flag to track image load
// (C) ADD WATERMARK IMAGE
function mark () { if (ready==2) {
// (C1) CREATE CANVAS
const canvas = createCanvas(sFile.width, sFile.height),
ctx = canvas.getContext("2d");
// (C2) CALCULATE CENTER COORDINATES
var sX = Math.floor(sFile.naturalWidth/2 - sMark.naturalWidth/2),
sY = Math.floor(sFile.naturalHeight/2 - sMark.naturalHeight/2);
// (C3) DRAW SOURCE + WATERMARK IMAGES
ctx.drawImage(sFile, 0, 0);
ctx.drawImage(sMark, sX, sY);
// (C4) SAVE
const out = fs.createWriteStream(sSave),
stream = canvas.createPNGStream();
stream.pipe(out);
out.on("finish", () => console.log("Done"));
}}
// (D) LOAD SOURCE + WATERMARK IMAGES
loadImage(sFile).then(img => {
sFile = img; ready++; mark();
});
loadImage(sMark).then(img => {
sMark = img; ready++; mark();
});
Finally, centering the watermark unfortunately involves some Math. We need to calculate the center coordinates of where to place the watermark.
X = FLOOR((IMAGE WIDTH - WATERMARK WIDTH) ÷ 2)
Y = FLOOR((IMAGE HEIGHT - WATERMARK HEIGHT) ÷ 2)
EXTRA BITS & LINKS
That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.
LINKS & REFERENCES
- Node Canvas – GitHub
- Node Canvas – NPMJS
- Canvas API – MDN
INFOGRAPHIC CHEAT SHEET

THE END
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!