2

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:

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:

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('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.

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:

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.

Update My guess is the string isn't displayed using gm because it comes through stderr, not 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 ?

Community
  • 1
  • 1
George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • Hi George, Sorry, I don't speak `node` but I think this is very similar - maybe? http://stackoverflow.com/a/43222531/2836621 – Mark Setchell Apr 11 '17 at 13:18
  • If the problem is that you want to use `identify` rather than `convert`, you can make `convert` act like `identify` (and thereby use the latter's calling interface) by using `gm convert someImage.jpg info:-` optionally adding in `-verbose` if needed. – Mark Setchell Apr 11 '17 at 13:20
  • @MarkSetchell thank you very much for the input. I have adapted your suggestion to node:```gm(jpgPath) .command('convert') .write('out.png', function(err, stdout,stderr){ console.log("stderr",stderr); });``` and it works! If you add your comment as an answer I'd be more than happy to accept :) – George Profenza Apr 11 '17 at 14:04
  • @MarkSetchell Thank you. (Btw, I don't speak `node` either, I play by ear :P) – George Profenza Apr 11 '17 at 14:45

1 Answers1

2

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

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.

You can do that like this:

gm convert someImage.jpg info:-

optionally adding in -verbose if needed.

That looks like this in node apparently!

gm(jpgPath)
  .command('convert')
  .write('out.png', function(err, stdout,stderr){   
     console.log("stderr",stderr);
});
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432