How to do composite with gm node.js?

核能气质少年 提交于 2019-11-28 21:29:19

问题


How to do 'gm composite -gravity center change_image_url base_image_url' with GM Node.js?

How to call gm().command() & gm().in() or gm().out() to achieve the above?


回答1:


After struggling for an hour, here is my solution for your question:

gm composite -gravity center change_image_url base_image_url

gm()
.command("composite") 
.in("-gravity", "center")
.in(change_image_url)
.in(base_image_url)
.write( output_file, function (err) {
  if (!err) 
    console.log(' hooray! ');
  else
    console.log(err);
});

Good luck! Hope it will be helpful to others as well :)




回答2:


Install gm, (make sure you already install graphicsmagick

npm install gm

following is my example code to merge two image together (use gm.in)

var gm = require('gm');

gm()
 .in('-page', '+0+0')
 .in('bg.jpg')
 .in('-page', '+10+20') // location of smallIcon.jpg is x,y -> 10, 20
 .in('smallIcon.jpg')
 .mosaic()
 .write('tesOutput.jpg', function (err) {
    if (err) console.log(err);
 });



回答3:


i am doing that this way:

var exec = require('child_process').exec
var command = [
        '-composite',
        '-watermark', '20x50',
        '-gravity', 'center', 
        '-quality', 100,
        'images/watermark.png',
        'images/input.jpg', //input
        'images/watermarked.png'  //output
        ];
         // making watermark through exec - child_process
        exec(command.join(' '), function(err, stdout, stderr) { 
            if (err) console.log(err);

        });



回答4:


Why does nobody use composite command? (https://github.com/aheckmann/gm)

var gm = require('gm');
var bgImage = 'bg.jpg',
    frontImage = 'front.jpg',
    resultImage = 'result.jpg',
    xy = '+100+150';

gm(bgImage)
  .composite(frontImage)
  .geometry(xy)
  .write(resultImage, function (err) {
    if (!err) console.log('All done');
  });

UPDATE Oh, I watched history of source of this method. It becomes available only on 2014




回答5:


If you want to resize and merge, you can use this:

gm()
.in('-geometry', '+0+0')
.in('./img/img1.png')
.in('-geometry', '300x300+100+200')
.in('./img/img2.png')
.flatten()
.write('resultGM.png', function (err) {
  if (err) console.log(err);
});



回答6:


Having the pleasure of being confined to a windows machine for the moment I solved this problem eventually by not using the "gm" module at all. For some reason, even though I have installed graphics-magick via its installer the node module refused to find it in my environment variables. But that could have something to do with the fact that I am trying to make an application with Electron.js (which is similar to Node.js but has its "gotcha's").

var exec = require("child_process").execSync;
var commandArray = [
    __dirname + "/bin/graphicsMagick/gm.exe",    // the relative path to the graphics-magick executable
    "-composite",
    "-gravity",
    "center",
    __dirname + "/data/images/qr/logo-small.png",    // relative paths to the images you want to composite
    __dirname + "/data/images/qr/qr-webpage.png",
    __dirname + "/data/images/qr/qr-webpage-logo.png"    // relative path to the result
];
var returnValue = exec(commandArray.join(" "));

For windows I think this is the correct portable way to do it.



来源:https://stackoverflow.com/questions/18463559/how-to-do-composite-with-gm-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!