如何在 node.js gm 库中嵌套 imagemagick 命令?
How to nest imagemagick commands in node.js gm library?
通过 gm 库 (https://github.com/aheckmann/gm) 在 node.js 中使用 Imagemagick:
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% \)")
传递嵌套命令的正确方法是什么?
我真的无法解决 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
通过 gm 库 (https://github.com/aheckmann/gm) 在 node.js 中使用 Imagemagick:
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% \)")
传递嵌套命令的正确方法是什么?
我真的无法解决 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