如何使用node.js和gm检查损坏的jpg图像?

时间:2022-11-19 14:44:14

I'd like to check for corrupted jpeg images and so far, straight in the command line I can use identify image.jpg which outputs:

我想检查损坏的jpeg图像,到目前为止,直接在命令行中我可以使用识别image.jpg输出:

image.jpg JPEG 1920x1200 1920x1200+0+0 8-bit sRGB 65.5KB 0.000u 0:00.009
identify: Premature end of JPEG file `image.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
identify: Corrupt JPEG data: premature end of data segment `image.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.

or gm identify image.jpg which outputs:

或gm识别image.jpg输出:

image.jpg JPEG 1920x1200+0+0 DirectClass 8-bit 64.0Ki 0.000u 0:01
gm identify: Corrupt JPEG data: premature end of data segment (image.jpg).
    gm identify: Corrupt JPEG data: premature end of data segment (image.jpg).

It would be nice if I can use the gm package to get the corrupt JPEG data too. Simply using identify() outputs a lot of data, but nothing about corrupt data

如果我可以使用gm包来获取损坏的JPEG数据,那将是很好的。简单地使用identify()输出大量数据,但没有任何关于损坏数据的信息

gm('image.jpg')
.identify('%C',function (err, data) {
  if (!err) console.log(data)
  else console.error(err)
});

I've noticed this note in the readme:

我在自述文件中注意到了这个注释:

If gm does not supply you with a method you need or does not work as you'd like, you can simply use gm().in() or gm().out() to set your own arguments.

如果gm没有为您提供您需要或不能按照您的方式工作的方法,您可以简单地使用gm()。in()或gm()。out()来设置您自己的参数。

I've tried something like this:

我尝试过这样的事情:

gm()
.command("identify") 
.in('image.jpg');

but I get no output so I'm probably doing it wrong.

但我没有输出所以我可能做错了。

I've also tried node-cmd:

我也尝试过node-cmd:

cmd.get(
        'gm identify image.jpg',
        function(data){
            console.log('output: ',data)
        }
    );

but I only see the first line of the output.

但我只看到输出的第一行。

What's the clean/recommended way of getting the multiline output from identify via gm package ? Otherwise, what's a node elegant solution to reading the full output of the identify command.

什么是通过gm包识别多行输出的干净/推荐方法?否则,什么是读取identify命令的完整输出的节点优雅解决方案。

Update My guess is the string isn't displayed using gm because it comes through stderr, not stdout.

更新我的猜测是使用gm不显示字符串,因为它来自stderr,而不是stdout。

I've tested using a tweaked version of this snippet:

我已经使用此片段的调整版本进行了测试:

var spawn = require('child_process').spawn;

var bin = "identify"
var args = ['image.jpg'];
var cspr = spawn(bin, args);
cspr.stderr.on('data', function (data) {
    data += '';
    console.log(data.replace("\n", "\nstderr: "));
});
cspr.on('exit', function (code) {
    console.log('child process exited with code ' + code);
    process.exit(code);
});

What's the clean way of getting the stderr output via gm ?

通过gm获取stderr输出的干净方法是什么?

1 个解决方案

#1


2  

I am very unqualified to say anything much about node or Javascript, but the following idea may get you up and running.

我没有资格对节点或Javascript说些什么,但以下想法可能会让你开始运行。

As I understand it, you want to use identify but are unable to capture its stderr. We do know how to capture the stderr of convert though. So, the suggestion is to invoke convert in a way it mimics the functionality of identify but with the calling interface of convert.

据我了解,你想使用识别,但无法捕获它的stderr。我们知道如何捕获转换的stderr。因此,建议是以一种模仿识别功能的方式调用转换,但使用转换的调用接口。

You can do that like this:

你可以这样做:

gm convert someImage.jpg info:-

optionally adding in -verbose if needed.

如果需要,可选择添加-verbose。

That looks like this in node apparently!

显然在节点中看起来像这样!

gm(jpgPath)
  .command('convert')
  .write('out.png', function(err, stdout,stderr){   
     console.log("stderr",stderr);
});

#1


2  

I am very unqualified to say anything much about node or Javascript, but the following idea may get you up and running.

我没有资格对节点或Javascript说些什么,但以下想法可能会让你开始运行。

As I understand it, you want to use identify but are unable to capture its stderr. We do know how to capture the stderr of convert though. So, the suggestion is to invoke convert in a way it mimics the functionality of identify but with the calling interface of convert.

据我了解,你想使用识别,但无法捕获它的stderr。我们知道如何捕获转换的stderr。因此,建议是以一种模仿识别功能的方式调用转换,但使用转换的调用接口。

You can do that like this:

你可以这样做:

gm convert someImage.jpg info:-

optionally adding in -verbose if needed.

如果需要,可选择添加-verbose。

That looks like this in node apparently!

显然在节点中看起来像这样!

gm(jpgPath)
  .command('convert')
  .write('out.png', function(err, stdout,stderr){   
     console.log("stderr",stderr);
});