我有一些限制,我不会指定要求我使用ImageMagick作为子进程。
我有多个基本的64个jpg文件字符串,我希望ImageMagick处理。 具体来说,我希望ImageMagick将jpg文件加入到一起。如果我有2个常规jpg文件,那么从命令行我将使用以下格式。
node convert in_1.jpg in_2.jpg +append out.jpg
在js文件中我会使用
var spawn, magicCommands, imagic;
spawn = require('child_process').spawn;
magicCommands = ["in_1.jpg",
"in_2.jpg",
"+append",
"out.jpg"];
imagic = spawn("convert", magicCommands);
现在,如果我想使用 1 stdin缓冲区,则以下方法可以正常工作
var arrow1JpgBase64, arrow2JpgBase64, arrowBuffer1, arrowBuffer2, magicCommands, imagic;
spawn = require('child_process').spawn;
exec = require('child_process').exec;
arrow1JpgBase64 = "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBwcHBw8LCwkMEQ8SEhEPERETFhwXExQaFRERGCEYGh0dHx8fExciJCIeJBweHx7/2wBDAQUFBQcGBw4ICA4eFBEUHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh4eHh7/wAARCAAFAAkDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1R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AMBAAIRAxEAPwD1/wCOvgjxJc/Dt7NPit4tMt1q+k20Ty2+nqsLyajbIkoMFtFJuRmDjbIpyo5xmu5/4Q3xH/0Vjxn/AOAuk/8AyDRRQB//2Q==";
arrowBuffer1 = new Buffer(arrow1JpgBase64, 'base64');
arrowBuffer2 = new Buffer(arrow2JpgBase64, 'base64');
magicCommands = ["jpg:",
"in_2.jpg",
"+append",
"out.jpg"];
imagic = spawn("convert", magicCommands);
imagic.stdin.write(arrowBuffer1);
imagic.stdin.end();
imagic.on('exit', function (code) {
if (code === 0) {
exec("open out.jpg");
} else {
console.log("error code: " + code);
}
}); // end of on exit
到目前为止一切顺利,但我想使用两个缓冲区,而不仅仅是 1 。因此,如果我将"in_2.jpg",
行替换为"jpg:",
,那么如何更改脚本的其余部分才能使其生效?
由于
答案 0 :(得分:3)
Mark Setchell在评论中指出,使用ImageMagick的fd:
协议将起作用。
var spawnOptions = {
stdio: [
0, // stdin,
1, // stdout
2, // stderr
'pipe', // arrowBuffer1
'pipe' // arrowBuffer2
]
};
magic = spawn("convert", magicCommands, spawnOptions);
这会打开fd:3
& fd:4
用于管道。我对node.js系列没有失败,但除了pipe之外,通常还有一种方法可以传递资源。
代码
更新magickCommands
变量以从新fd
中读取,并直接写入新的pipes
magicCommands = ["fd:3",
"fd:4",
"+append",
"out.jpg"];
// ...
imagic.stdio[3].write(arrowBuffer1);
imagic.stdio[4].write(arrowBuffer2);
imagic.stdio[3].end();
imagic.stdio[4].end();
答案 1 :(得分:1)
不确定这些"限制" 是什么,或者我们试图避免或解决的问题,但以下将多个文件合并为一个文件的技术可以帮助您:
# Make a red block
convert -size 50x50 xc:red multi.miff
# Make a green block, but APPEND INTO A SINGLE STREAM
convert -size 50x50 xc:lime miff:- >> multi.miff
# Make a blue block, but APPEND INTO A SINGLE STREAM
convert -size 50x50 xc:blue miff:- >> multi.miff
# Tell IM to convert and append multiple images in single stream
convert multi.miff +append result.png
此外,如果您将上面的上一个命令更改为以下命令,IM会在完成后立即删除文件multi.miff
- 即为您整理!
convert ephemeral:multi.miff +append result.png