'use strict'; /** * Crops the month plates in src/images/ (removes the paper margin, the thin * frame and the printed caption) and writes web-sized JPGs to * public/assets/months/.jpg. * * Not a project dependency: run with `npm i --no-save sharp && node scripts/prepare-images.cjs`. */ const fs = require('node:fs'); const path = require('node:path'); const sharp = require('sharp'); const SRC_DIR = path.join(__dirname, '..', 'src', 'images'); const OUT_DIR = path.join(__dirname, '..', 'public', 'assets', 'months'); // Pixels to remove around the engraving (the scans differ by a few pixels in width). const CROP = { left: 62, top: 62, right: 66, bottom: 225 }; const OUTPUT_WIDTH = 560; async function main() { fs.mkdirSync(OUT_DIR, { recursive: true }); for (const file of fs.readdirSync(SRC_DIR).filter((name) => name.endsWith('.jpg'))) { const source = path.join(SRC_DIR, file); const { width, height } = await sharp(source).metadata(); await sharp(source) .extract({ left: CROP.left, top: CROP.top, width: width - CROP.left - CROP.right, height: height - CROP.top - CROP.bottom, }) .resize({ width: OUTPUT_WIDTH }) .jpeg({ quality: 80, mozjpeg: true }) .toFile(path.join(OUT_DIR, file.toLowerCase())); } } main();