0

Using Imagemagick in node.js through the gm library (https://github.com/aheckmann/gm):

var gm = require('gm').subClass({ imageMagick: true });

I am able to flatten pictures, like this:

convert img1.png img2.png img3.png -flatten out.png

with this js code:

gm().command("convert").in("img1.png").in("img2.png").in("img3.png").in("-flatten").toBuffer('PNG' ...

but now I want to tint one of the images, like this:

convert img1.png \( img2.png -fill green -colorize 50% \) img3.png -flatten out.png

but I had not succeed, I tried:

.in("(img2.png -fill green -colorize 50% )")
.in("\(img2.png -fill green -colorize 50% \)")

what is the proper way to pass nested commands?

4

1 回答 1

0

我真的无法用 gm 解决问题,据了解检查文档和源代码,在图书馆的当前状态下一步是不可能的。然后我找到了 Jimp 库:它是纯 javascript,而且功能更少,实现所需的行为非常简单。与 AWS Lambda 中的 node.js 配合得很好。在一个缓冲区中应用色调,然后将它们混合在一起,如下所示:

//each skin_buffer[attr] is a Jimp object build like this: 
     Jimp.read(data.Body, function (err, image) {
        skin[type] = image; 
        cb(err);
      });

//this is how the tint effect is applied in Jimp:
  skin_buffers.hair.color([
    { apply: 'mix', params: [ '#00D', 50 ] }
  ]);
//.. and this is the composite step. skin is just another jimp object 
  skin.composite(skin_buffers.hair, 0, 0);
  skin.composite(skin_buffers.legs, 0, 0);
  skin.composite(skin_buffers.shoes, 0, 0); 
  skin.composite(skin_buffers.torso, 0, 0); 
  skin.composite(skin_buffers.edges, 0, 0); 
  skin.composite(skin_buffers.face, 0, 0); 
  skin.getBuffer( Jimp.MIME_PNG, function(err, buffer){
    cb(err, buffer);
  });

注意: jimp.mix 太慢了!一张 1024x1024 的图像需要 13 秒!:

[skins_create]got all the assets!
TIMER: got assets from s3: 3.193 sg
[skins_create]["skin","face","hair","torso","legs","shoes","edges"]
 - SUBTIMER : jimp.mix: 13.002
 - SUBTIMER : jimp.composite hair: 0.14
 - SUBTIMER : jimp.composite legs: 0.145
 - SUBTIMER : jimp.composite shoes: 0.15
 - SUBTIMER : jimp.composite torso: 0.147
 - SUBTIMER : jimp.composite face: 0.146
 - SUBTIMER : jimp.get buffer: 0.45
TIMER: processed buffers: 14.185 sg
TIMER: stored in s3: 4.21 sg
于 2017-05-08T14:19:18.417 回答