Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge 2 images with node.js?

Tags:

node.js

image

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.

like image 375
Mikael Sundberg Avatar asked Nov 09 '11 19:11

Mikael Sundberg


People also ask

Is Node JS good for image processing?

Node. js has an ecosystem of libraries you can use to process images, such as sharp, jimp, and gm module.


2 Answers

You might need this: https://github.com/zhangyuanwei/node-images

Cross-platform image decoder(png/jpeg/gif) and encoder(png/jpeg) for Nodejs

images("big.jpg").draw(images("small.jpg"), 10, 10).save("output.jpg");
like image 191
user2122440 Avatar answered Sep 18 '22 16:09

user2122440


I do no have enough reputation to add a comment, or else I would to Schmidko's answer.

Sharp works well, however, overlayWith is deprecated and you instead need to use composite. See below:

sharp(path + 'example.jpg')
       .composite([{input: path + 'logo.png', gravity: 'southeast' }])
       .toFile(path + 'output.png');

If you would like to center the image being overlayed: gravity: 'centre'

like image 34
ZachHappel Avatar answered Sep 18 '22 16:09

ZachHappel