gpt4 book ai didi

javascript - nodejs 中 JIMP 中的文件签名无效

转载 作者:行者123 更新时间:2023-11-30 15:01:56 26 4
gpt4 key购买 nike

我正在使用 JIMP 将我的图像转换为灰度并降低其质量。但在 2% 的情况下,它会破坏我的图像并在控制台中抛出错误-“错误:无效的文件签名 在 Parser._parseSignature (C:\Users\Akshay\Desktop\darwin\node_modules\pngjs\lib\parser.js:50:18)"下面是有问题的代码:

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

最佳答案

我遇到了同样的问题,尽管我们尽了最大努力,但似乎问题是之前的文件仍未完成写入。

所以我以一种愚蠢、可耻的方式做到了这一点,并添加了半秒的延迟以防读取失败。

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

关于javascript - nodejs 中 JIMP 中的文件签名无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46404120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com