2017-09-25 4 views
1

이미지를 그레이 스케일로 변환하고 품질을 낮추기 위해 JIMP를 사용하고 있습니다. 그러나 2 %의 경우 이미지가 손상되어 콘솔에 오류가 발생했습니다. "오류 : Parser._parseSignature에서 잘못된 파일 서명 (C : \ 사용자 \하기 Akshay \ 바탕 화면 \ 다윈 \ node_modules \ pngjs \ lib 디렉토리 \ parser.js : 50 : 18) " 다음 경우 문제가있는 코드 :JIMP의 노드 j에서 유효하지 않은 파일 서명

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); 
     }); 
     } 

답변

1

나는 똑같은 문제에 직면 해 있으며, 최선의 노력에도 불구하고 이전 파일이 아직 작성이 끝난 것 같지 않습니다.

그래서 나는 바보 같은 부끄러운 길에서 그것을했고, 읽기가 실패 할 경우를 대비하여 0.5 초의 지연을 추가했습니다.

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(); 
    } 
} 
관련 문제