Image 将半透明水印应用于Node.js中的图像
我有一个图像,希望使用Node.js在其上应用水印(另一个图像)。要求是水印不应该是一张实心图片(我可以用gm library的draw()来做),而应该是半透明的。您能建议使用什么工具吗?通过派生子进程从命令行使用ImageMagickImage 将半透明水印应用于Node.js中的图像,image,node.js,graphics,imagemagick,Image,Node.js,Graphics,Imagemagick,我有一个图像,希望使用Node.js在其上应用水印(另一个图像)。要求是水印不应该是一张实心图片(我可以用gm library的draw()来做),而应该是半透明的。您能建议使用什么工具吗?通过派生子进程从命令行使用ImageMagick // Require our module dependencies var exec = require('child_process').exec; // Create command array to invoke ImageMagick compos
// 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
});
如果您需要一个API抽象,这里有一个很棒的模块通过派生子进程从命令行使用ImageMagick
// 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
});
如果您需要API抽象,这里有一个很棒的模块您可以将水印图像制作成半透明的,并与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?
});
您可以将水印图像设置为半透明,并与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?
});
我使用代码
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?
});
错误:命令失败:转换:不符合绘图原语定义“/path/to/half-transparent watermark image.png”@draw.c/DrawImage/3124我使用代码
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?
});
错误:命令失败:转换:不符合绘图原语定义“/path/to/half-transparent watermark image.png”@draw.c/DrawImage/3124OK。两个月后你引起了我的注意。我有一段时间没有访问stackoverflow.com,因为我很忙。所以,你说我的代码不起作用。首先,我的代码是抽象的。“/path/to/”意味着您必须替换为要在gm中使用的文件的真实路径。接下来,配置node.js应用程序和依赖项(包括graphics magick的本机库)非常重要,请确保您安装了所有必要的库(包)。我使用Ubuntu操作系统(有时也使用Debian)在服务器和台式电脑上,我安装了带有“sudo apt get install graphicsmagick”的Graphics Magick。听着,我是个实践者,我从不在没有经过验证的代码的情况下说话。在写这篇文章之前,我编写了一个示例应用程序,以确保这段代码仍然有效(可能情况发生了变化,而这段代码不再有效)。但这里是:查看“app.js”和“res/out.jpg”(从谷歌硬盘下载该档案)。我希望这对你有帮助。两个月后你引起了我的注意。我有一段时间没有访问stackoverflow.com,因为我很忙。所以,你说我的代码不起作用。首先,我的代码是抽象的。“/path/to/”意味着您必须替换为要在gm中使用的文件的真实路径。接下来,配置node.js应用程序和依赖项(包括graphics magick的本机库)非常重要,请确保您安装了所有必要的库(包)。我使用Ubuntu操作系统(有时也使用Debian)在服务器和台式电脑上,我安装了带有“sudo apt get install graphicsmagick”的Graphics Magick。听着,我是个实践者,我从不在没有经过验证的代码的情况下说话。在写这篇文章之前,我编写了一个示例应用程序,以确保这段代码仍然有效(可能情况发生了变化,而这段代码不再有效)。但这里是:查看“app.js”和“res/out.jpg”(从谷歌硬盘下载该档案)。我希望,这会对你有所帮助。