Apply half-transparent watermark to image in Node.

2019-06-04 06:43发布

I have an image and would like to apply a watermark (another image) over it using Node.js. The requirement is watermark shouldn't be a solid picture (what I can do with gm library's draw()) but half transparent. Can you please advise any tool to use?

3条回答
爷的心禁止访问
2楼-- · 2019-06-04 07:03

Use ImageMagick from the command line by forking a child-process

// Require our module dependencies
var exec = require('child_process').exec;

// Create command array to invoke ImageMagick composite where
// -dissolve is the amount of transparency for the watermark
// -gravity tells how to align images of varying size
// -quality is the image quality of the JPEG (not required if producing PNG)
var command = [
    'composite',
    '-dissolve', '50%',
    '-gravity', 'center', 
    '-quality', 100,
    '/path/to/watermark.png',
    '/path/to/image.jpg',
    '/path/to/save/result.jpg';
];

// Join command array by a space character and then execute command
exec(command.join(' '), function(err, stdout, stderr) {
    // Do stuff with result here
});

If you need an API abstraction, there is a great module found here https://github.com/rsms/node-imagemagick

查看更多
Explosion°爆炸
3楼-- · 2019-06-04 07:10

You could make watermark image half-transparent already and use it with gm:

gm('/path/to/input-image.jpg')
.draw(['image Over 0,0 0,0 /path/to/half-transparent-watermark-image.png'])
.write('/path/to/output-image.jpg', function(e){
 console.log(e||'done'); // What would you like to do here?
});
查看更多
姐就是有狂的资本
4楼-- · 2019-06-04 07:21

I use code

gm('/path/to/input-image.jpg')
.draw(['image Over 0,0 0,0 /path/to/half-transparent-watermark-image.png'])
.write('/path/to/output-image.jpg', function(e){
 console.log(e||'done'); // What would you like to do here?
});

Error: Command failed: convert: Non-conforming drawing primitive definition `/path/to/half-transparent-watermark-image.png''@ draw.c / DrawImage / 3124

查看更多
登录 后发表回答