将多个图像流通过管道传输到 ImageMagick/GraphicsMagick 个子进程

Pipe multiple image streams into ImageMagick/GraphicsMagick child process

我能够将一个图像流通过管道传输到 ImageMagick 子进程中。我的代码基于 Pipe stream to graphicsmagick/imagemagick child process

我想合成多个图像流并将合成图像作为响应通过管道传输。这是我的 Node.js 代码,它从我的 AWS S3 检索图像存储桶,然后通过管道将其作为响应输出:

app.get('/composite_images', function(req, res){    
    res.writeHead(200, {'Content-Type' : 'image/png'});

    s3.get("/" + "fronttop.png").on("response", function(s3Res) {
      // '-'' means standard in or stdin
      var args = ['-', '-page', '+0+0', '-mosaic', '-'];
      var convert = spawn("convert", args);

      // This worked!
      s3Res.pipe(convert.stdin);

      convert.stdout.pipe(res);
      convert.stderr.pipe(process.stderr);
    }).end();
})

官方documentation

"At this time IM does not allow you to just read one image from such a stream, process it, and then read another single image. All IM commands will always read the whole stream, and then close it. This is being fixed as part of IMv7 scripted processing."

是否可以使用GraphicsMagick/ImageMagick合成多个图像流?

不确定这是否是您的意思,但请看一下。 ImageMagick 确实有一种 流格式 ,它被称为 MIFF 或 Magick 图像文件格式。它允许图像通过管道一个接一个地流式传输。我不会说 node 但这是它在命令行中的样子:

{ convert -size 50x50 xc:red miff:- ; convert -size 50x50 xc:blue miff:- ; } | convert miff:- +append result.png

基本上,{} 是一个包含 2 个 ImageMagick convert 进程的复合语句,第一个以 MIFF 格式将红色方形图像写入其标准输出,第二个写入标准输出的蓝色方块,也是 MIFF 格式。然后这两张图片通过管道传输,并由第三台 convert 从标准输入中读取红色和蓝色方块并将它们并排连接起来。

以下是您可以如何在循环中使用它。小心不要在这个过程的中间将任何调试语句放在标准输出上 - 曾经做过并且花了很长时间试图弄清楚为什么会破坏东西的人说!

#!/bin/bash
for c in red green blue cyan magenta yellow; do 
  convert -size 50x50 xc:$c miff:-
done | convert miff:- +append result.png

也许你现在可以看到如何做你想做的事...