-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·49 lines (39 loc) · 1.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import fse from 'fs-extra';
import { ImagePool } from '@squoosh/lib';
import { cpus } from 'os';
import path from 'path';
const imagePool = new ImagePool(cpus().length);
const imgFilePaths = process.argv[2].split('\t').map((str) => str.trim());
const preprocessOptions = {
resize: {
enabled: true,
width: Number(process.env.width),
height: Number(process.env.height),
},
};
(async () => {
for (const imgFilePath of imgFilePaths) {
try {
const imgFile = await fse.readFile(imgFilePath);
const imgArrayBuf = imagePool.ingestImage(imgFile);
if (process.env.width || process.env.height) {
console.log('Image resized.');
await imgArrayBuf.decoded;
await imgArrayBuf.preprocess(preprocessOptions);
}
const filename = (path.basename(imgFilePath)).split('.')[0];
await imgArrayBuf.encode({
[process.env.format]: {
'quality': Number(process.env.quality)
}
});
const dst = path.resolve(process.env.dest ?? path.dirname(imgFilePath), filename + `.${process.env.format}`);
await fse.writeFile(dst, (await imgArrayBuf.encodedWith[process.env.format]).binary);
} catch (err) {
console.error(err);
continue;
}
}
console.log('Jobs done successfully!');
await imagePool.close();
})();