Compare commits
9 Commits
feature-si
...
main
Author | SHA1 | Date | |
---|---|---|---|
|
ca10f8a038 | ||
00e5fed522 | |||
|
91ab1a453c | ||
|
3eb527008a | ||
007da37674 | |||
|
0427a3295f | ||
c852633119 | |||
|
ead2b1a7ee | ||
|
84034ef41c |
62
poptimal.js
62
poptimal.js
@ -2,6 +2,7 @@ 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";
|
||||||
@ -17,9 +18,11 @@ import {ColourNamer} from './src/components/colour_namer.js';
|
|||||||
|
|
||||||
const CELL = 10;
|
const CELL = 10;
|
||||||
const MAG = 2;
|
const MAG = 2;
|
||||||
const WIDTH = 20;
|
const WIDTH = 200;
|
||||||
const HEIGHT = WIDTH;
|
const HEIGHT = WIDTH;
|
||||||
const VISIBLE_DOG = 1000;
|
// number of pixels which have to be visible for a colour to be
|
||||||
|
// 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()));
|
||||||
@ -33,13 +36,37 @@ function randomise_params() {
|
|||||||
f: random.choice(RADIUS_OPTS),
|
f: random.choice(RADIUS_OPTS),
|
||||||
r: random.float(0, 0.4),
|
r: random.float(0, 0.4),
|
||||||
}});
|
}});
|
||||||
|
const cell = 5 + random.float() * random.float() * 55;
|
||||||
return {
|
return {
|
||||||
background: palette[0],
|
background: palette[0],
|
||||||
palette: palette_name,
|
palette: palette_name,
|
||||||
patterns: patterns
|
patterns: patterns,
|
||||||
|
cell: cell,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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!
|
||||||
|
|
||||||
@ -109,18 +136,21 @@ function poptimal_svg(params) {
|
|||||||
const document = window.document;
|
const document = window.document;
|
||||||
const container = d3.select(document.body).append("div");
|
const container = d3.select(document.body).append("div");
|
||||||
|
|
||||||
const dm = new DotMaker(WIDTH);
|
const width = WIDTH / params.cell;
|
||||||
|
const height = WIDTH / params.cell;
|
||||||
|
|
||||||
|
const dm = new DotMaker(width, height);
|
||||||
|
|
||||||
const svg = container.append("svg")
|
const svg = container.append("svg")
|
||||||
.attr("width", WIDTH * CELL * MAG)
|
.attr("width", WIDTH * MAG)
|
||||||
.attr("height", HEIGHT * CELL * MAG)
|
.attr("height", HEIGHT * MAG)
|
||||||
.attr("viewBox", [ 0, 0, WIDTH, HEIGHT ]);
|
.attr("viewBox", [ 0, 0, width, height ]);
|
||||||
|
|
||||||
const background = svg.append("rect")
|
const background = svg.append("rect")
|
||||||
.attr("x", 0)
|
.attr("x", 0)
|
||||||
.attr("y", 0)
|
.attr("y", 0)
|
||||||
.attr("width", WIDTH)
|
.attr("width", width)
|
||||||
.attr("height", WIDTH)
|
.attr("height", height)
|
||||||
.attr("fill", params.background);
|
.attr("fill", params.background);
|
||||||
|
|
||||||
|
|
||||||
@ -183,20 +213,22 @@ 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.png -c config.json")
|
.usage("Usage: -s SIZE [-o output] -c config.json [-p params.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 fn = argv.o || String(Date.now()) + '.png';
|
const jsfn = (argv.o || ts) + '.json';
|
||||||
|
|
||||||
const imgfile = cf['working_dir'] + '/' + fn;
|
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 colourf = params.palette === 'grayscale' ? cf['grayscale'] : cf['colour'];
|
||||||
|
|
||||||
const namer = new ColourNamer();
|
const namer = new ColourNamer();
|
||||||
@ -216,14 +248,14 @@ 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
|
||||||
// so we don't include obscured colours
|
// so we don't include obscured colours
|
||||||
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);
|
||||||
|
params.alt_text = alt_text;
|
||||||
|
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'] ) {
|
||||||
|
@ -45,7 +45,6 @@ function int_range(v1, v2) {
|
|||||||
|
|
||||||
class DotMaker {
|
class DotMaker {
|
||||||
constructor(width, height) {
|
constructor(width, height) {
|
||||||
console.log(width, height);
|
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.wh = 0.5 * (width + height);
|
this.wh = 0.5 * (width + height);
|
||||||
|
@ -144,8 +144,8 @@ const dots2 = dm.dots(1 / m2, n2, false);
|
|||||||
|
|
||||||
|
|
||||||
const svg = d3.create("svg")
|
const svg = d3.create("svg")
|
||||||
.attr("width", width * cell * MAG)
|
.attr("width", WIDTH * MAG)
|
||||||
.attr("height", height * cell * MAG)
|
.attr("height", HEIGHT * MAG)
|
||||||
.attr("viewBox", [ 0, 0, width, height ]);
|
.attr("viewBox", [ 0, 0, width, height ]);
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user