kollagen/cli/main.ts

94 lines
2.5 KiB
TypeScript
Raw Normal View History

import { parse } from "https://deno.land/std@0.106.0/flags/mod.ts";
2021-09-18 18:57:38 +02:00
import { createCanvas, init } from "https://deno.land/x/canvas@v1.3.0/mod.ts";
import { CollageModeType, collageModeType } from "../src/common/collages.ts";
2021-09-15 21:15:41 +02:00
import DenoCollageModes, {
CastCanvasRenderingContext,
ProxyImage,
} from "./collages.ts";
import { CollageConfig } from "../src/common/types.ts";
import { choice, shuffle } from "../src/common/utils.ts";
2021-09-18 19:04:29 +02:00
import { walkSync } from "https://deno.land/std@0.107.0/fs/mod.ts";
const canvasKit = await init();
const args = parse(Deno.args, {
2021-09-15 21:15:41 +02:00
alias: {
"w": "width",
"h": "height",
2021-09-15 21:15:41 +02:00
"o": "output",
"m": "mode",
2021-09-18 19:04:29 +02:00
"r": "recursive",
2021-09-15 21:15:41 +02:00
},
2021-09-18 19:04:29 +02:00
boolean: ["recursive"]
});
2021-09-18 18:58:16 +02:00
if (args["mode"] === true) {
2021-09-18 12:11:03 +02:00
console.log(collageModeType.join(", "));
Deno.exit(0);
}
2021-09-18 19:04:29 +02:00
const files: Set<string> = new Set();
const includeExtensions = Array.from(
String(args["include"] || "*.png, *.jpg").matchAll(/\*\.([\w]+)/g),
).map(([_, group]) => group);
2021-09-17 18:56:57 +02:00
args["_"].forEach((arg) => {
arg = arg.toString();
if (Deno.statSync(arg).isDirectory) {
2021-09-18 19:04:29 +02:00
Array.from(
walkSync(arg, {
maxDepth: args["recursive"]
? Infinity
: 1,
includeDirs: false,
includeFiles: true,
exts: includeExtensions.length ? includeExtensions : undefined,
}),
).forEach((entry) => files.add(entry.path));
2021-09-17 18:56:57 +02:00
} else {
2021-09-18 19:04:29 +02:00
files.add(arg);
2021-09-17 18:56:57 +02:00
}
});
2021-09-18 19:04:29 +02:00
if (files.size < 2) {
2021-09-15 21:15:41 +02:00
console.error("kollagen needs at least 2 images to work.");
Deno.exit(1);
}
2021-09-18 19:04:29 +02:00
const shuffledFiles = shuffle(Array.from(files));
if (shuffledFiles.length > 25) {
console.debug(`Too many files available, shuffling and clamping to 25...`);
shuffledFiles.splice(25, shuffledFiles.length - 25);
}
2021-09-19 11:46:49 +02:00
const images: ProxyImage[] = shuffledFiles.map((file) => new ProxyImage(file));
const modes = new DenoCollageModes();
const modeKey: CollageModeType = args["mode"] || choice(collageModeType);
const mode = modes.modes[modeKey];
2021-09-15 21:15:41 +02:00
console.log(
`Creating a "${mode.name}" collage...`,
2021-09-15 21:15:41 +02:00
);
2021-09-18 19:04:29 +02:00
console.debug(`Images: ${shuffledFiles.join(", ")}`);
const canvas = createCanvas(args["width"] || 640, args["height"] || 640);
const context = canvas.getContext("2d");
const collageConfig: CollageConfig = {
2021-09-15 21:15:41 +02:00
numImages: args["n"],
};
2021-09-15 21:15:41 +02:00
const segments = mode.getSegments(
context as CastCanvasRenderingContext,
collageConfig,
images,
2021-09-15 21:15:41 +02:00
);
mode.place(context as CastCanvasRenderingContext, images, segments);
const output = args["output"] || "collage.png";
console.log(`Saving to "${output}"...`);
2021-09-15 21:15:41 +02:00
await Deno.writeFile(output, canvas.toBuffer());