61 lines
1.6 KiB
JavaScript
61 lines
1.6 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const libReimage = require('./libReimage');
|
|
|
|
const inputDir = './input';
|
|
const outputDir = './output';
|
|
|
|
const globalOptions = {
|
|
format: 'jpg',
|
|
outputDpi: 300,
|
|
jpegQuality: 85,
|
|
};
|
|
|
|
const reimg = libReimage(globalOptions);
|
|
|
|
const sizes = [
|
|
{ suffix: 'small', megapixels: 0.5 },
|
|
{ suffix: 'medium', megapixels: 2 },
|
|
{ suffix: 'large', megapixels: 8 },
|
|
];
|
|
|
|
fs.mkdirSync(outputDir, { recursive: true });
|
|
|
|
(async () => {
|
|
const files = fs.readdirSync(inputDir);
|
|
|
|
for (const file of files) {
|
|
const inputPath = path.join(inputDir, file);
|
|
|
|
if (!(await reimg.isImage(inputPath))) {
|
|
console.warn(`Skipping non-image or unreadable file: ${file}`);
|
|
continue;
|
|
}
|
|
|
|
try {
|
|
const img = await reimg.load(inputPath);
|
|
|
|
// Deliberately JPEG-condition the source once, then branch every output
|
|
// size from those same JPEG-derived pixels.
|
|
const jpg = await reimg.forceJpg(img);
|
|
const baseName = path.parse(file).name;
|
|
|
|
for (const size of sizes) {
|
|
const color = await reimg.resizeMegaPixel(jpg, size.megapixels);
|
|
const gray = await reimg.toGrayscale(color);
|
|
|
|
const colorName = `${baseName}.${size.suffix}.color.jpg`;
|
|
const grayName = `${baseName}.${size.suffix}.gray.jpg`;
|
|
|
|
await reimg.saveAs(color, path.join(outputDir, colorName));
|
|
console.log(`✓ ${file} → ${colorName}`);
|
|
|
|
await reimg.saveAs(gray, path.join(outputDir, grayName));
|
|
console.log(`✓ ${file} → ${grayName}`);
|
|
}
|
|
} catch (err) {
|
|
console.error(`Error processing ${file}: ${err.message}`);
|
|
}
|
|
}
|
|
})();
|