我正在使用GraphicsMagick for node拍摄源图像、调整大小、裁剪然后应用蒙版:
gm(tempfile)
.quality(90)
.resize(null, 38)
.gravity('Center')
.crop(20, 34)
.mask('./public/assets/mask.png')
.write(thumb, function (err) {
if (err) throw err
console.log('success')
})
运行后,图像已成功调整大小并裁剪,但未应用蒙版。不会抛出任何错误(即控制台打印“成功”)。
附加的也是我尝试使用的蒙版图像。我希望图像只绘制在黑色部分。我尝试过使用透明 png(gm 文档说它基于 alpha channel 进行 mask )以及黑白 jpg,但结果是相同的。
我确信我错过了一些明显的东西,但我很困惑,谢谢!
最佳答案
又过了一天左右,我已经弄清楚了:
Mask 本身没有任何作用,真的没什么用。它仅获取提供的蒙版图像,并使用它来写入保护蒙版像素免受后续更改(如果对图像执行其他处理/绘制)。
因为 Node GM 不支持复合,所以我的解决方案是使用系统调用应用掩码。因为似乎没有任何方法可以在一个步骤中将裁剪和合成结合起来( graphicsmagick composite and crop in the same command ),所以我分两步完成了:
var exec = require('child_process').exec
gm = require('gm')
gm(tempfile)
.quality(90)
.resize(null, thumbOffset)
.gravity('Center')
.crop(thumbWidth, thumbHeight)
.write(thumb, function (err) {
if (err) throw err
console.log('thumb sized')
compositeMask(thumb, mask, function(){
console.log('mask1 done')
})
})
function compositeMask(thumb, mask, next) {
var gmComposite = 'gm composite -compose in ' + thumb + ' ' + mask + ' ' + thumb
exec(gmComposite, function(err) {
if (err) throw err
pathUpdate(entryID, { thumb: thumb })
next()
})
}
关于javascript - GraphicsMagick 用于 Node 不屏蔽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20828951/