我正在尝试将多个图像叠加到一个图像上。它在一定程度上有效,但它没有按照我想要的方式工作。
global.images = [];
fs.readdirSync('./images').forEach(function(file) {
images.push(file);
if (debug) {
console.log(chalk.green('[Debug] Pushed ' + chalk.blue(file) + ' to the array.'));
}
});
executeEdit(images);
function executeEdit(list) {
var jimps = [];
var x = 0;
for (var i = 0; i < images.length; i++) {
x = x + 150;
setTimeout((function(i) {
return function() {
jimps.push(jimp.read('images/' + images[i]));
}
})(i), 10 * x);
}
此代码引发以下错误: (node:15352) UnhandledPromiseRejectionWarning: Error: unexpected end of file at Inflate.zlibOnError (zlib.js:153:15) at Inflate._processChunk (D:\ImageTest\node_modules\pngjs\lib\sync-inflate.js:110:28) at zlibBufferSync (D:\ImageTest\node_modules\pngjs\lib\sync-inflate.js:151:17) at inflateSync (D:\ImageTest\node_modules\pngjs\lib\sync-inflate.js:155:10) at module.exports (D:\ImageTest\node_modules\pngjs\lib\parser-sync.js:79:20) at Object.exports.read [as image/png] (D:\ImageTest\node_modules\pngjs\lib\png-sync.js:10:10) at Jimp.parseBitmap (D:\ImageTest\node_modules\@jimp\core\dist\utils\image-bitmap.js:117:53) at Jimp.parseBitmap (D:\ImageTest\node_modules\@jimp\core\dist\index.js:498:32) at D:\ImageTest\node_modules\@jimp\core\dist\index.js:440:15 at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3) (node:15352) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
我不知道为什么。这可能是由于从目录中读取并将其添加到数组中,因为var images = ['file.png', 'file2.png' ]
工作正常。
如果有人对如何修复有任何想法,请告诉我。
谢谢!
编辑:这样做有效,但它只选择最后一个图像而不是任何其他图像:/
for (var i = 0; i < images.length; i++) {
x = x + 150;
// console.log(i);
var image = {
_integer: i,
_image: images[i],
func: function() {
jimps.push(jimp.read('images/' + this._image));
// console.log(this._integer);
}
}
setTimeout(function() {image.func()}, x);
}
提示是消息UnhandledPromiseRejectionWarning
:您错过了jimp
实际异步的事实(回调或承诺)。在您的情况下,由于没有传递给jimp.read()
它的回调假定使用了承诺,但您没有正确使用它们。
如果您替换// console.log(this._integer);
为console.log(jimps)
,您将看到如下内容:
[ Promise { <pending> } ]
[ Promise { <Jimp 32x32> }, Promise { <pending> } ]
....
我完全不知道你在用jimps
数组做什么(也不知道你为什么在setTimeout()
这里使用 a ),但你也许可以做这样的事情(如果通过删除一些日志和删除setTimeout()
内容来简化一下)。这是现在正确的承诺代码。
var fs = require('fs');
var jimp = require('jimp');
const images = [];
fs.readdirSync('./images').forEach(function(file) {
images.push(file);
console.log('[Debug] Pushed', file, ' to the array');
});
executeEdit(images);
console.log('waiting for executeEdit() to terminate');
function executeEdit(list) {
let jimp_read_promises = []
for (var i = 0; i < images.length; i++) {
jimp_read_promises.push( jimp.read('images/' + images[i]))
}
Promise.all(jimp_read_promises).then( loadedimgs => {
loadedimgs.map( img => {
console.log( img )
})
})
}
所以这将打印以下内容
[Debug] Pushed 7035a55d06033e435be112c0969b1820.png to the array
[Debug] Pushed aa5ef861d490a11fe20806e83c6dc64b.png to the array
[Debug] Pushed crayon.png to the array
waiting for executeEdit() to terminate
<Jimp 32x32>
<Jimp 48x48>
<Jimp 256x256>
因此,同步读取 3 个文件(在我的情况下)然后调用 executeEdit()。我们构建了一个 promise 对象的数组,然后调用 Promise.all() ,它会等到所有的 promise 都得到解决。该值loadedimgs
是一个包含每个承诺结果的数组(与承诺的顺序相同)。然后,您可以根据需要遍历该数组(这里我使用了一个map
调用,但是 for 循环就可以了。
我希望这有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句