Jimp: Invalid file signature in JIMP in nodejs

Created on 25 Sep 2017  Â·  11Comments  Â·  Source: oliver-moran/jimp

I am using JIMP to convert my image to greyscale and to reduce its quality.But for 2% of the cases it is corrupting my image and throwing an error in the console- "Error: Invalid file signature at Parser._parseSignature (C:UsersAkshayDesktopdarwinnode_modulespngjs libparser.js:50:18)" Below if the problematic code:
I am using JIMP to convert my image to greyscale and to reduce its quality.But for 2% of the cases it is corrupting my image and throwing an error in the console-
"Error: Invalid file signature
at Parser._parseSignature (C:UsersAkshayDesktopdarwinnode_modulespngjs
libparser.js:50:18)"
Below if the problematic code:

 var ext=path.extname(dest);
      if(ext!='.jpg'){
       dest=replaceExt(dest, '.jpg');
      }
      console.log(path.extname(dest));
      var file = fs.createWriteStream(dest);
      ////console.log(url)
      if(url.indexOf('https')!=-1){
        //console.log("https")
        var request = https.get(url, function(response) {
        response.pipe(file);
        file.on('finish', function() {
          Jimp.read(dest).then(function (lennaa) {
            lennaa.resize(256, 256)            // resize
              .quality(90)                 // set JPEG quality
              .greyscale()                 // set greyscale
              .write(dest); // save
          }).catch(function (err) {
            console.error(err);
          });
          file.close(cb);  // close() is async, call cb after close completes.
        });
      }).on('error', function(err) { // Handle errors
        fs.unlink(dest); // Delete the file async. (But we don't check the result)
        if (cb) cb(err.message);
      });
      }
bug

Most helpful comment

My solution was (shame!!) to add a half-second delay if the first read fails, and retry the read after that delay.

All 11 comments

+1

Were you able to find a solution?

Not till now.

My solution was (shame!!) to add a half-second delay if the first read fails, and retry the read after that delay.

@catcher-in-the-try can you tell me, how to do it?

@k1ker I added the code on StackOverflow: https://stackoverflow.com/questions/46404120/invalid-file-signature-in-jimp-in-nodejs

Copied here:

let image;
try {
  image = await Jimp.read(filepath);
} catch (error) {
  debug(`Error reading ${filepath}. But we'll try again!`);

  // Wait half second, try again. What an ugly hack. It might as well work.
  let temp = await new Promise(resolve => setTimeout(resolve, 600));
  try {
    image = await Jimp.read(filepath);
    debug('Success reading file on second attempt!');
  } catch (error2) {
    // If totally failed, at least exit gracefully:
    this.session.send('The bot is a little busy now. Please try again.');
    this.session.endDialog();
  }
}

The key part is the delay:

  let temp = await new Promise(resolve => setTimeout(resolve, 600));

Patch not a solution.This brings to the area which is not yet explored.Will start developing a new library to replace JIMP.

Did you try with "close" instead of "finish" ?
file.on('close' function() { Jimp.read(dest) [...] })

Works for me.

Can you anwer, how after
>

image = await Jimp.read(filepath);

I can use function (err, lenna)? Now, it's like: Jimp.read(template,
function (err, lenna) {

2017-10-24 5:29 GMT+03:00 Anton Ivanov notifications@github.com:

@k1ker https://github.com/k1ker I added the code on StackOverflow:
https://stackoverflow.com/questions/46404120/invalid-
file-signature-in-jimp-in-nodejs

Copied here:

let image;try {
image = await Jimp.read(filepath);
} catch (error) {
debug(Error reading ${filepath}. But we'll try again!);

// Wait half second, try again. What an ugly hack. It might as well work.
let temp = await new Promise(resolve => setTimeout(resolve, 600));
try {
image = await Jimp.read(filepath);
debug('Success reading file on second attempt!');
} catch (error2) {
// If totally failed, at least exit gracefully:
this.session.send('The bot is a little busy now. Please try again.');
this.session.endDialog();
}
}

The key part is the delay:

let temp = await new Promise(resolve => setTimeout(resolve, 600));

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/oliver-moran/jimp/issues/349#issuecomment-338853372,
or mute the thread
https://github.com/notifications/unsubscribe-auth/Admfa8WuJhAfyCNS72e-2P-tXt52FO4Vks5svUuMgaJpZM4Pildc
.

@akshayseth can you provide a minimal example repo? thanks!

Since I have not seen any response on this I'm gonna close it cause I cannot reproduce the issue. @Durss seems to have a solution. Feel free to open another issue if it persists!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chancein007 picture chancein007  Â·  5Comments

molipha picture molipha  Â·  6Comments

SamuelZhaoY picture SamuelZhaoY  Â·  3Comments

fatihturgut picture fatihturgut  Â·  4Comments

tutyamxx picture tutyamxx  Â·  4Comments