2017-05-05 54 views
0

通過通用庫(https://github.com/aheckmann/gm)在node.js中使用ImageMagick:如何在node.js gm庫中嵌入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% \)") 

什麼是通過嵌套命令的正確方法?

+0

GraphicsMagick是不一樣的ImageMagick。很久以前,他們分開並分開去了那裏。對不起,我不知道GraphicsMagick或Node.js – fmw42

+0

thks @ fmw42!我剛剛編輯了這個問題,強調了JavaScript的gm庫實際上使用了ImageMagic。 – cesarpachon

回答

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是太慢了!對於一個單一的圖像需要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 
+0

我能夠實現更快的版本的Jimp.mix ..問題是,Jimp使用每像素回調,並在每個回調內創建對象,再加上從tinycolor2庫調用更多的回調..只是創建一個普通的函數,由複製tinycolor2和jimp的代碼,沒有回調,它加速到0.03 sg,而不是14個:) – cesarpachon