Compare commits
No commits in common. "ead2b1a7ee2eb9caf1d1645e16e7bf3327c62c74" and "0fbe462779f23c6bc63de1fd058646db18c47330" have entirely different histories.
ead2b1a7ee
...
0fbe462779
40
poptimal.js
40
poptimal.js
@ -2,7 +2,6 @@ import { Resvg } from "@resvg/resvg-js";
|
|||||||
import { promises } from "fs";
|
import { promises } from "fs";
|
||||||
import { JSDOM } from "jsdom";
|
import { JSDOM } from "jsdom";
|
||||||
import * as d3 from "d3";
|
import * as d3 from "d3";
|
||||||
import * as d3c from "d3-color";
|
|
||||||
import yargs from "yargs/yargs";
|
import yargs from "yargs/yargs";
|
||||||
import { hideBin } from "yargs/helpers";
|
import { hideBin } from "yargs/helpers";
|
||||||
import spawn from "await-spawn";
|
import spawn from "await-spawn";
|
||||||
@ -20,9 +19,7 @@ const CELL = 10;
|
|||||||
const MAG = 2;
|
const MAG = 2;
|
||||||
const WIDTH = 20;
|
const WIDTH = 20;
|
||||||
const HEIGHT = WIDTH;
|
const HEIGHT = WIDTH;
|
||||||
// number of pixels which have to be visible for a colour to be
|
const VISIBLE_DOG = 1000;
|
||||||
// mentioned in the alt text
|
|
||||||
const VISIBLE_DOG = 400;
|
|
||||||
|
|
||||||
function randomise_params() {
|
function randomise_params() {
|
||||||
const palette_name = random.choice(Array.from(PALETTES.keys()));
|
const palette_name = random.choice(Array.from(PALETTES.keys()));
|
||||||
@ -43,28 +40,6 @@ 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!
|
// lol the best way I found to to this was imagemagick!
|
||||||
|
|
||||||
@ -208,26 +183,23 @@ async function post_image(image, alt_text, cf) {
|
|||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const argv = yargs(hideBin(process.argv))
|
const argv = yargs(hideBin(process.argv))
|
||||||
.usage("Usage: -s SIZE [-o output] -c config.json [-p params.json]")
|
.usage("Usage: -s SIZE -o output.png -c config.json")
|
||||||
.default('s', 1200)
|
.default('s', 1200)
|
||||||
.default('c', 'config.json').argv;
|
.default('c', 'config.json').argv;
|
||||||
|
|
||||||
const cfjson = await promises.readFile(argv.c);
|
const cfjson = await promises.readFile(argv.c);
|
||||||
const cf = JSON.parse(cfjson);
|
const cf = JSON.parse(cfjson);
|
||||||
|
|
||||||
const ts = String(Date.now());
|
|
||||||
|
|
||||||
const fn = (argv.o || ts) + '.png';
|
|
||||||
const jsfn = (argv.o || ts) + '.json';
|
const fn = argv.o || String(Date.now()) + '.png';
|
||||||
|
|
||||||
const imgfile = cf['working_dir'] + '/' + fn;
|
const imgfile = cf['working_dir'] + '/' + fn;
|
||||||
const paramsfile = cf['working_dir'] + '/' + jsfn;
|
|
||||||
|
|
||||||
const params = await load_or_random_params(argv.p);
|
const params = randomise_params();
|
||||||
const colourf = params.palette === 'grayscale' ? cf['grayscale'] : cf['colour'];
|
const colourf = params.palette === 'grayscale' ? cf['grayscale'] : cf['colour'];
|
||||||
|
|
||||||
const namer = new ColourNamer();
|
const namer = new ColourNamer();
|
||||||
console.log(`Loading colours ${colourf}`);
|
|
||||||
await namer.load_colours(colourf);
|
await namer.load_colours(colourf);
|
||||||
|
|
||||||
|
|
||||||
@ -244,6 +216,7 @@ async function main() {
|
|||||||
const pngData = resvg.render();
|
const pngData = resvg.render();
|
||||||
const pngBuffer = pngData.asPng();
|
const pngBuffer = pngData.asPng();
|
||||||
|
|
||||||
|
|
||||||
await promises.writeFile(imgfile, pngBuffer);
|
await promises.writeFile(imgfile, pngBuffer);
|
||||||
|
|
||||||
// generate the alt_text last to check the image file histogram
|
// generate the alt_text last to check the image file histogram
|
||||||
@ -251,7 +224,6 @@ async function main() {
|
|||||||
const hist = await get_histogram(imgfile);
|
const hist = await get_histogram(imgfile);
|
||||||
const alt_text = image_description(namer, params, hist);
|
const alt_text = image_description(namer, params, hist);
|
||||||
|
|
||||||
await save_params(paramsfile, params);
|
|
||||||
console.log(alt_text);
|
console.log(alt_text);
|
||||||
console.log(imgfile);
|
console.log(imgfile);
|
||||||
if( cf['base_url'] ) {
|
if( cf['base_url'] ) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user