使用Sharp设置不透明度的叠加



我想拍摄一张图像,并将其合成在另一张图像上。

这很好地实现了这个目的:

overlayImg = await Sharp(sourceImage.Body)
.composite([{ 
input: './lambdas/processNewImage/logos/white.png', 
gravity: 'southeast',

}])
.toFormat('jpeg').toBuffer();

我还有一个变量1-100,应该是水印的不透明度。有时我想要它完全固体在100%不透明,其他70%和其他30%。。。等等。因为我需要它是可变的,所以我不能仅仅改变水印图像的不透明度。

我不知道如何在Sharp中更改合成图像的不透明度。

有人能举个简单的例子吗?

在另一个线程中发现了这一点,在该线程中,您覆盖了一个包含平铺半透明像素值的原始像素缓冲区。128表示50%的不透明度,其中有效值为0-255。https://github.com/lovell/sharp/issues/554

overlayImg = await Sharp(sourceImage.Body)
.composite([
{ 
input: './lambdas/processNewImage/logos/white.png', 
gravity: 'southeast',  
},
{
input: Buffer.from([0,0,0,128]),
raw: {
width: 1,
height: 1,
channels: 4,
},
tile: true,
blend: 'dest-in',
}
])
.toFormat('jpeg').toBuffer();

Sharp可以更改图像的alpha通道(不透明度(。https://sharp.pixelplumbing.com/api-channel#ensurealpha

sharp(current_path + 'logo.png')
.ensureAlpha(0.5) // 50% opacity 
.toBuffer()
.then(transparent_logo=>{
sharp(current_path + 'photo.png')
.composite([{input: transparent_logo }])
.toFile(current_path + 'output.png');
})

最新更新