Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 8, 2023 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/877824042a5e0b77d0321c122bc903d9 to your computer and use it in GitHub Desktop.
Save code-boxx/877824042a5e0b77d0321c122bc903d9 to your computer and use it in GitHub Desktop.
NodeJS Write Text To Image

NODEJS WRITE TEXT TO IMAGE

https://code-boxx.com/nodejs-add-text-to-image/

NOTES

  1. Change all registerFont(PATH/TO/FONT) to your own.
  2. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Save the image below as demo.png
    • Install required modules - npm i canvas
    • Run the examples.

IMAGES

demo

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// (A) LOAD MODULES
const fs = require("fs"),
{ createCanvas, loadImage } = require("canvas");
// (B) SETTINGS
const
sFile = "demo.png", // source image
sSave = "demoA.png", // "save as"
sText = "FRIED RICE", // text to write
sX = 80, sY = 80; // text position
// (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 TEXT ONTO IMAGE
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) LOAD MODULES
const fs = require("fs"),
{ registerFont, createCanvas, loadImage } = require("canvas");
// (B) SETTINGS - CHANGE FONT TO YOUR OWN!
const
sFile = "demo.png", // source image
sSave = "demoB.png", // "save as"
sText = "FRIED RICE", // text to write
sX = 80, sY = 80; // text position
registerFont("C:/Windows/Fonts/arialbd.ttf", { family: "Arial Bold" }); // CHANGE TO YOUR OWN!
// (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 TEXT ONTO IMAGE
ctx.font = '36px "Arial Bold"';
ctx.fillStyle = "rgba(255, 0, 0, 0.4)";
ctx.lineWidth = 2;
ctx.strokeStyle = "rgb(0, 0, 0)";
ctx.fillText(sText, sX, sY);
ctx.strokeText(sText, sX, sY);
// (C4) SAVE
const out = fs.createWriteStream(sSave),
stream = canvas.createPNGStream();
stream.pipe(out);
out.on("finish", () => console.log("Done"));
});
// (A) LOAD MODULES
const fs = require("fs"),
{ registerFont, createCanvas, loadImage } = require("canvas");
// (B) SETTINGS - CHANGE FONT TO YOUR OWN!
const
sFile = "demo.png", // source image
sSave = "demoC.png", // "save as"
sText = "FRIED RICE"; // text to write
registerFont("C:/Windows/Fonts/arialbd.ttf", { family: "Arial Bold" }); // CHANGE TO YOUR OWN!
// (C) LOAD IMAGE + DRAW TEXT
loadImage(sFile).then(img => {
// (C1) CREATE CANVAS + DRAW IMAGE
const canvas = createCanvas(img.width, img.height),
ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
// (C2) TEXT DIMENSIONS
ctx.font = '36px "Arial Bold"';
ctx.fillStyle = "rgba(255, 0, 0, 0.4)";
ctx.lineWidth = 2;
ctx.strokeStyle = "rgb(0, 0, 0)";
let td = ctx.measureText(sText),
tw = td.width,
th = td.actualBoundingBoxAscent + td.actualBoundingBoxDescent;
// (C3) CALCULATE CENTER & WRITE ON CENTER
let x = Math.floor((img.naturalWidth - tw) / 2),
y = Math.floor((img.naturalHeight + th) / 2);
ctx.strokeText(sText, x, y);
ctx.fillText(sText, x, y);
// (C4) SAVE
const out = fs.createWriteStream(sSave),
stream = canvas.createPNGStream();
stream.pipe(out);
out.on("finish", () => console.log("Done"));
});
curl https://user-images.githubusercontent.com/11156244/275809280-249d7e13-6969-4318-b4f4-104e09964c07.png --ssl-no-revoke --output demo.png
call npm i canvas
node 1-simple.js
node 2-more.js
node 3-center.js
curl https://user-images.githubusercontent.com/11156244/275809280-249d7e13-6969-4318-b4f4-104e09964c07.png --ssl-no-revoke --output ./demo.png
source "npm i canvas"
node ./1-simple.js
node ./2-more.js
node ./3-center.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment