Node.js相当新,尝试使用Jimp(https://www.npmjs.com/package/jimp)进行图像处理。
但是,当我尝试以下示例时:
var Jimp = require('jimp');
// open a file called "myImage.png"
Jimp.read('myImage.png', (err, myImage) => {
if (err) throw err;
lenna
.resize(256, 256) // resize
.quality(60) // set JPEG quality
.greyscale() // set greyscale
.write('myImage-small-bw.jpg'); // save
});
我收到以下错误:
{ AssertionError [ERR_ASSERTION]: zlib binding closed
at Inflate._processChunk (/Users/.../node_modules/pngjs/lib/sync-inflate.js:108:3)
at zlibBufferSync (/Users/.../node_modules/pngjs/lib/sync-inflate.js:151:17)
at inflateSync (/Users/.../node_modules/pngjs/lib/sync-inflate.js:155:10)
at module.exports (/Users/.../node_modules/pngjs/lib/parser-sync.js:85:20)
at Object.exports.read [as image/png] (/Users/.../node_modules/pngjs/lib/png-sync.js:10:10)
at Jimp.parseBitmap (/Users/.../node_modules/@jimp/core/dist/utils/image-bitmap.js:117:53)
at Jimp.parseBitmap (/Users/.../node_modules/@jimp/core/dist/index.js:498:32)
at /Users/.../node_modules/@jimp/core/dist/index.js:440:15
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:490:3)
generatedMessage: false,
name: 'AssertionError [ERR_ASSERTION]',
code: 'ERR_ASSERTION',
actual: undefined,
expected: true,
operator: '==',
methodName: 'constructor' }
我尝试进行研究,但未成功。
我确实尝试过写方法:
new Jimp(256, 256, (err, myImage) => {
image.write('myImage-small-bw.jpg');
});
效果很好。任何帮助都将受到高度赞赏。
编辑:经过一些测试,我注意到它仅不适用于PNG ...