Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6781385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:37:04+00:00 2026-05-26T16:37:04+00:00

I want to merge 2 images using node.js. Or rather, i want to place

  • 0

I want to merge 2 images using node.js. Or rather, i want to place one smaller image on cordinates x,y on a larger image.
Even more precise: I have an image of glasses, and an image of a face and i want to put the glasses on the face.
I did some googling, and found some image manipulating libraries, but none seem to be able to merge images.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:37:05+00:00Added an answer on May 26, 2026 at 4:37 pm

    I’ve used:

    https://github.com/learnboost/node-canvas

    to do something similar (build a composite image from components on the fly).

    It works great.

    Here’s some example code:

    var Canvas = require('canvas'),
      fs = require('fs'),
      Image = Canvas.Image;
    
    
    
    var _components = [{prefix:'f', count:12},
                       {prefix:'h', count:12},
                       {prefix:'i', count:12},
                       {prefix:'m', count:12}];
    
    
    var _total = 1;
    for (var i=_components.length - 1; i>=0; i--){
      _components[i].mult = _total;
      _total *= _components[i].count;
    }
    
    
    module.exports.ensureImageExists = function(img, cb){
      fs.stat(__dirname + '/../public/images/rb/' + img, function(err, stats){
        if (err){
          if (err.code == 'ENOENT')
            generateImage(img, cb);
          else
            cb(err);
        }
        else{
          cb();
        }
      });
    }
    
    function generateImage(name, cb){
      var re = /rb([0-9]*)\.png/
    
      var num = parseInt(re.exec(name)[1]) % _total;
    
      var parts = [];
      for (var i=0; i<_components.length; i++){
        var n = Math.floor(num / _components[i].mult);
        parts.push(_components[i].prefix + (n + 1));
        num -= n * _components[i].mult;
      }
    
      var canvas = new Canvas(45, 45),
       ctx = canvas.getContext('2d');
    
      drawParts();
    
      function drawParts(){
        var part = parts.shift();
        if (!part)
          saveCanvas();
        else {
          var img = new Image;
          img.onload = function(){
            ctx.drawImage(img, 0, 0, 45, 45);
            drawParts();
          };
         img.src = __dirname + '/components/' + part + '.png';
        }
      }
    
      function saveCanvas(){
        canvas.toBuffer(function(err, buf){
          if (err)
            cb(err);
          else
            fs.writeFile(__dirname + '/../public/images/rb/' + name, buf, function(){
              cb();
            });
        });
      }
    
    }
    

    In this case, the components are selected based upon the name of the image, but you clearly could do otherwise. Also, I imagine you could just stream the image out if you wanted — I write it to a file so it’s available the next time it’s requested.

    I put a route like this in to handle the generation:

    app.get('/images/rb/:img', requireLogin, function(req, res, next){
      //first make sure image exists, then pass along so it is handled
      //by the static router
      rbgen.ensureImageExists(req.params.img, function(err){
        next();
      })
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to merge multiple images ina single image,by using a button click>my problem
I have 2 images and I want to merge them into one on my
I want to merge 3 images using PHP. I want to drag 2 images
i want to render 30 different images. Each task has to merge different image
Simple idea: I have two images that I want to merge, one is 500x500
I want to merge two images where one is a greeting card with a
I want to merge this queries into only one, to return 2 values (scount_atp
I want to merge two cells in excel using pyExcelerator , ws.write_merge(r1=0,r2=1,c1=0, c2=0, label='test1',
I want to merge one .NET DLL assembly and one C# Class Library project
i have two bitmap images ..and i need to merge the bitmaps with precise

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.