From ead2b1a7ee2eb9caf1d1645e16e7bf3327c62c74 Mon Sep 17 00:00:00 2001 From: Mike Lynch Date: Tue, 1 Apr 2025 01:48:28 +0000 Subject: [PATCH] Can now load params from the JSON --- poptimal.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/poptimal.js b/poptimal.js index bbbb058..84bbae1 100644 --- a/poptimal.js +++ b/poptimal.js @@ -2,6 +2,7 @@ import { Resvg } from "@resvg/resvg-js"; import { promises } from "fs"; import { JSDOM } from "jsdom"; import * as d3 from "d3"; +import * as d3c from "d3-color"; import yargs from "yargs/yargs"; import { hideBin } from "yargs/helpers"; import spawn from "await-spawn"; @@ -42,6 +43,28 @@ function randomise_params() { } } +async function load_or_random_params(paramf) { + if( paramf ) { + const pjson = await promises.readFile(paramf); + const params = JSON.parse(pjson); + params.background = d3c.color(params.background); + params.patterns.forEach((p) => { + p.colour = d3c.color(p.colour); + }); + return params; + } + return randomise_params(); +} + +async function save_params(paramf, params) { + params.background = params.background.formatHex(); + params.patterns.forEach((p) => { + p.colour = p.colour.formatHex(); + }); + await promises.writeFile(paramf, JSON.stringify(params)); +} + + // lol the best way I found to to this was imagemagick! @@ -185,7 +208,7 @@ async function post_image(image, alt_text, cf) { async function main() { const argv = yargs(hideBin(process.argv)) - .usage("Usage: -s SIZE -o output.png -c config.json") + .usage("Usage: -s SIZE [-o output] -c config.json [-p params.json]") .default('s', 1200) .default('c', 'config.json').argv; @@ -200,7 +223,7 @@ async function main() { const imgfile = cf['working_dir'] + '/' + fn; const paramsfile = cf['working_dir'] + '/' + jsfn; - const params = randomise_params(); + const params = await load_or_random_params(argv.p); const colourf = params.palette === 'grayscale' ? cf['grayscale'] : cf['colour']; const namer = new ColourNamer(); @@ -228,7 +251,7 @@ async function main() { const hist = await get_histogram(imgfile); const alt_text = image_description(namer, params, hist); - await promises.writeFile(paramsfile, JSON.stringify(params)); + await save_params(paramsfile, params); console.log(alt_text); console.log(imgfile); if( cf['base_url'] ) {