https://github.com/GoogleChromeLabs/squoosh/tree/dev/libsquoosh
please install:
npm install @squoosh/lib fs-extra
then run:
node ./squoosh-process-images.js
const { ImagePool } = require('@squoosh/lib') | |
const path = require('path') | |
const fs = require('fs-extra') | |
const srcFolder = path.join(__dirname, 'big') | |
const files = fs.readdirSync(srcFolder) | |
const outputFolder = path.join(__dirname, 'output') | |
fs.ensureDir(outputFolder) | |
fs.emptyDirSync(outputFolder) | |
const width = 400 | |
const height = 400 | |
const processImage = async (src, dest) => { | |
const imagePool = new ImagePool() | |
const imagePath = src | |
const image = imagePool.ingestImage(imagePath); | |
await image.decoded; //Wait until the image is decoded before running preprocessors. | |
const preprocessOptions = { | |
//When both width and height are specified, the image resized to specified size. | |
resize: { | |
enabled: true, | |
width, | |
height, | |
} | |
} | |
await image.preprocess(preprocessOptions); | |
const encodeOptions = { | |
mozjpeg: {}, //an empty object means 'use default settings' | |
jxl: { | |
quality: 90, | |
}, | |
} | |
await image.encode(encodeOptions); | |
await imagePool.close(); | |
const rawEncodedImage = (await image.encodedWith.mozjpeg).binary; | |
fs.writeFile(dest, rawEncodedImage); | |
} | |
const next = async (files) =>{ | |
if (files.length > 0) { | |
const f = files.shift() | |
console.log('processing', f) | |
await processImage(path.join(srcFolder, f), path.join(outputFolder, f)) | |
next(files) | |
} else { | |
console.log('end') | |
} | |
} | |
next(files) |
https://github.com/GoogleChromeLabs/squoosh/tree/dev/libsquoosh
please install:
npm install @squoosh/lib fs-extra
then run:
node ./squoosh-process-images.js