【问题标题】:How to nest imagemagick commands in node.js gm library?如何在 node.js gm 库中嵌套 imagemagick 命令?
【发布时间】:2017-10-10 16:49:37
【问题描述】:

通过 gm 库在 node.js 中使用 Imagemagick (https://github.com/aheckmann/gm):

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

我可以将图片展平,如下所示:

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

用这个js代码:

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

但现在我想为其中一张图片着色,如下所示:

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

但我没有成功,我尝试了:

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

传递嵌套命令的正确方法是什么?

【问题讨论】:

  • GraphicsMagick 与 ImageMagick 不同。很久以前,他们分道扬镳,分道扬镳。抱歉,我不知道 GraphicsMagick 或 Node.js
  • 感谢@fmw42!我刚刚编辑了问题以突出显示 javascript 的 gm 库实际上正在使用 ImageMagic。

标签: javascript node.js imagemagick imagemagick-convert gm


【解决方案1】:

我真的无法解决 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

【讨论】:

  • 我能够实现 Jimp.mix 的更快版本。问题是 Jimp 使用每个像素的回调,并在每个回调中创建对象,并从 tinycolor2 库中调用更多回调。只是创建一个普通函数,通过复制 tinycolor2 和 jimp 的代码,没有回调,它加速到 0.03 sg,而不是 14 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多