add cli params for recursive modes

This commit is contained in:
Tomáš Mládek 2021-09-20 22:56:02 +02:00
parent bfe739a487
commit e391b9b6bf
2 changed files with 38 additions and 17 deletions

View file

@ -4,12 +4,12 @@ import {
collageModeType,
DisplayCollageModeType,
displayCollageModeType,
isDisplayCollageModeType,
} from "../src/common/collages.ts";
import { denoCollageModes, ProxyImage } from "./collages.ts";
import { CollageConfig } from "../src/common/types.ts";
import { choice, shuffle } from "../src/common/utils.ts";
import { walkSync } from "https://deno.land/std@0.107.0/fs/mod.ts";
import { parseCollageModes, parseDisplayCollageModes } from "./util.ts";
const args = parse(Deno.args, {
alias: {
@ -18,11 +18,14 @@ const args = parse(Deno.args, {
o: "output",
m: "mode",
},
boolean: ["rr"],
default: {
w: 640,
h: 640,
include: "*.png, *.jpg",
output: "collage.png",
rl: 3,
rr: true
},
});
@ -61,18 +64,7 @@ const shuffledFiles = shuffle(Array.from(files));
const images: ProxyImage[] = shuffledFiles.map((file) => new ProxyImage(file));
const allModeKeys: DisplayCollageModeType[] = [];
if (args["mode"]) {
(args["mode"] as string).split(",").map((m) => m.trim()).forEach((m) => {
if (isDisplayCollageModeType(m)) {
allModeKeys.push(m);
} else {
throw Error(`"${m}" is not a valid collage mode.`);
}
});
} else {
allModeKeys.push(...displayCollageModeType);
}
const allModeKeys = args["mode"] ? parseDisplayCollageModes(args["mode"]) : displayCollageModeType;
const canvas = createCanvas(args["width"], args["height"]);
const context = canvas.getContext("2d");
@ -87,9 +79,9 @@ if (modeKey === "recursive") {
`Creating a recursive collage, choosing from ${shuffledFiles.length} files...`,
);
await denoCollageModes.recursiveDraw(context, images, {
modes: collageModeType,
repeat: true,
level: 3,
modes: args["rm"] ? parseCollageModes(args["rm"]) : collageModeType,
repeat: args["rr"],
level: args["rl"],
});
} else {
const mode = denoCollageModes.modes[modeKey];
@ -106,4 +98,4 @@ if (modeKey === "recursive") {
const output = args["output"];
console.log(`Saving to "${output}"...`);
await Deno.writeFile(output, canvas.toBuffer());
await Deno.writeFile(output, canvas.toBuffer());

29
cli/util.ts Normal file
View file

@ -0,0 +1,29 @@
import { CollageModeType, DisplayCollageModeType, isCollageModeType, isDisplayCollageModeType } from "../src/common/collages.ts";
export function parseDisplayCollageModes(input: string): DisplayCollageModeType[] {
const result: DisplayCollageModeType[] = [];
input.split(",").map((m) => m.trim()).forEach((m) => {
if (isDisplayCollageModeType(m)) {
result.push(m);
} else {
throw Error(`"${m}" is not a valid collage mode.`);
}
});
return result;
}
export function parseCollageModes(input: string): CollageModeType[] {
const result: CollageModeType[] = [];
input.split(",").map((m) => m.trim()).forEach((m) => {
if (isCollageModeType(m)) {
result.push(m);
} else {
throw Error(`"${m}" is not a valid collage mode.`);
}
});
return result;
}