Added webp image creation
This commit is contained in:
parent
05d0e3fbc0
commit
358dd3eac0
1 changed files with 24 additions and 1 deletions
|
|
@ -4,6 +4,9 @@
|
||||||
* 20260228 ChatGPT
|
* 20260228 ChatGPT
|
||||||
* $Header$
|
* $Header$
|
||||||
*
|
*
|
||||||
|
* This script takes a JSON job description and batch renders PNGs from GLBs
|
||||||
|
* using a headless browser and a local static server.
|
||||||
|
*
|
||||||
* Example:
|
* Example:
|
||||||
* node batch_render.js job.json
|
* node batch_render.js job.json
|
||||||
*/
|
*/
|
||||||
|
|
@ -14,10 +17,12 @@ import process from 'node:process';
|
||||||
import http from 'node:http';
|
import http from 'node:http';
|
||||||
import { pathToFileURL, fileURLToPath } from 'node:url';
|
import { pathToFileURL, fileURLToPath } from 'node:url';
|
||||||
import puppeteer from 'puppeteer';
|
import puppeteer from 'puppeteer';
|
||||||
|
import sharp from 'sharp';
|
||||||
|
|
||||||
function die (msg) { console.error(msg); process.exit(1); }
|
function die (msg) { console.error(msg); process.exit(1); }
|
||||||
function ensureDir(p) { fs.mkdirSync(p, { recursive: true }); }
|
function ensureDir(p) { fs.mkdirSync(p, { recursive: true }); }
|
||||||
|
|
||||||
|
|
||||||
function listFiles(dir, suffix) {
|
function listFiles(dir, suffix) {
|
||||||
const out = [];
|
const out = [];
|
||||||
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
for (const ent of fs.readdirSync(dir, { withFileTypes: true })) {
|
||||||
|
|
@ -102,6 +107,8 @@ async function main() {
|
||||||
const pattern = (job.pattern ?? ".glb").toLowerCase();
|
const pattern = (job.pattern ?? ".glb").toLowerCase();
|
||||||
|
|
||||||
ensureDir(outputDirAbs);
|
ensureDir(outputDirAbs);
|
||||||
|
const webpDirAbs = path.join(outputDirAbs, '../webp');
|
||||||
|
ensureDir(webpDirAbs);
|
||||||
|
|
||||||
const glbs = listFiles(inputDirAbs, pattern);
|
const glbs = listFiles(inputDirAbs, pattern);
|
||||||
if (glbs.length === 0) die(`No files ending with '${pattern}' found under ${inputDirAbs}`);
|
if (glbs.length === 0) die(`No files ending with '${pattern}' found under ${inputDirAbs}`);
|
||||||
|
|
@ -166,9 +173,25 @@ async function main() {
|
||||||
throw new Error(`No pngDataUrl produced for ${glbAbs} (status=${JSON.stringify(status)})`);
|
throw new Error(`No pngDataUrl produced for ${glbAbs} (status=${JSON.stringify(status)})`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//const b64 = status.pngDataUrl.replace(/^data:image\/png;base64,/, '');
|
||||||
|
//fs.writeFileSync(outAbs, Buffer.from(b64, 'base64'));
|
||||||
|
//console.log(`WROTE\t${outAbs}`);
|
||||||
const b64 = status.pngDataUrl.replace(/^data:image\/png;base64,/, '');
|
const b64 = status.pngDataUrl.replace(/^data:image\/png;base64,/, '');
|
||||||
fs.writeFileSync(outAbs, Buffer.from(b64, 'base64'));
|
const pngBuffer = Buffer.from(b64, 'base64');
|
||||||
|
|
||||||
|
/* write PNG */
|
||||||
|
fs.writeFileSync(outAbs, pngBuffer);
|
||||||
console.log(`WROTE\t${outAbs}`);
|
console.log(`WROTE\t${outAbs}`);
|
||||||
|
|
||||||
|
/* write WEBP */
|
||||||
|
const webpName = sanitizeBasename(glbAbs).replace(/\.glb$/i, '.webp');
|
||||||
|
const webpAbs = path.join(webpDirAbs, webpName);
|
||||||
|
|
||||||
|
await sharp(pngBuffer)
|
||||||
|
.webp({ quality: 92 })
|
||||||
|
.toFile(webpAbs);
|
||||||
|
|
||||||
|
console.log(`WROTE\t${webpAbs}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
await page.close();
|
await page.close();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue