2021-09-15 19:08:53 +02:00
|
|
|
import { parse } from "https://deno.land/std@0.106.0/flags/mod.ts";
|
2021-09-19 12:12:45 +02:00
|
|
|
import { createCanvas } from "https://deno.land/x/canvas@v1.3.0/mod.ts";
|
2021-09-20 22:38:04 +02:00
|
|
|
import {
|
2021-09-20 22:41:00 +02:00
|
|
|
collageModeType,
|
|
|
|
DisplayCollageModeType,
|
|
|
|
displayCollageModeType,
|
|
|
|
} from "../src/common/collages.ts";
|
|
|
|
import { denoCollageModes, ProxyImage } from "./collages.ts";
|
2021-09-15 19:08:53 +02:00
|
|
|
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";
|
2021-09-20 22:56:02 +02:00
|
|
|
import { parseCollageModes, parseDisplayCollageModes } from "./util.ts";
|
2021-09-15 19:08:53 +02:00
|
|
|
|
|
|
|
const args = parse(Deno.args, {
|
2021-09-15 21:15:41 +02:00
|
|
|
alias: {
|
2021-09-20 22:40:12 +02:00
|
|
|
w: "width",
|
|
|
|
h: "height",
|
|
|
|
o: "output",
|
|
|
|
m: "mode",
|
2021-09-15 21:15:41 +02:00
|
|
|
},
|
2021-09-20 22:56:02 +02:00
|
|
|
boolean: ["rr"],
|
2021-09-20 22:40:12 +02:00
|
|
|
default: {
|
|
|
|
w: 640,
|
|
|
|
h: 640,
|
|
|
|
include: "*.png, *.jpg",
|
2021-09-20 22:41:00 +02:00
|
|
|
output: "collage.png",
|
2021-09-20 23:06:11 +02:00
|
|
|
rl: 2,
|
2022-01-19 21:26:06 +01:00
|
|
|
rr: false,
|
2021-09-20 22:41:00 +02:00
|
|
|
},
|
2021-09-15 19:08:53 +02:00
|
|
|
});
|
|
|
|
|
2021-09-18 18:58:16 +02:00
|
|
|
if (args["mode"] === true) {
|
2021-09-20 22:38:04 +02:00
|
|
|
console.log(displayCollageModeType.join(", "));
|
2021-09-18 12:11:03 +02:00
|
|
|
Deno.exit(0);
|
|
|
|
}
|
|
|
|
|
2021-09-18 19:04:29 +02:00
|
|
|
const files: Set<string> = new Set();
|
2021-09-18 11:42:44 +02:00
|
|
|
const includeExtensions = Array.from(
|
2022-01-19 21:26:06 +01:00
|
|
|
String(args["include"]).matchAll(/\*\.([\w]+)/g)
|
2021-09-18 11:42:44 +02:00
|
|
|
).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, {
|
2021-09-20 22:43:25 +02:00
|
|
|
maxDepth: Infinity,
|
2021-09-18 19:04:29 +02:00
|
|
|
includeDirs: false,
|
|
|
|
includeFiles: true,
|
|
|
|
exts: includeExtensions.length ? includeExtensions : undefined,
|
2022-01-19 21:26:06 +01:00
|
|
|
})
|
2021-09-18 19:04:29 +02:00
|
|
|
).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-15 19:08:53 +02:00
|
|
|
}
|
|
|
|
|
2021-09-18 19:04:29 +02:00
|
|
|
const shuffledFiles = shuffle(Array.from(files));
|
2021-09-17 19:09:51 +02:00
|
|
|
|
2021-09-19 11:46:49 +02:00
|
|
|
const images: ProxyImage[] = shuffledFiles.map((file) => new ProxyImage(file));
|
2021-09-15 21:16:01 +02:00
|
|
|
|
2022-01-19 21:26:06 +01:00
|
|
|
const allModeKeys = args["mode"]
|
|
|
|
? parseDisplayCollageModes(args["mode"])
|
|
|
|
: displayCollageModeType;
|
2021-09-15 19:08:53 +02:00
|
|
|
|
2021-09-20 22:40:12 +02:00
|
|
|
const canvas = createCanvas(args["width"], args["height"]);
|
2021-09-15 19:08:53 +02:00
|
|
|
const context = canvas.getContext("2d");
|
|
|
|
|
|
|
|
const collageConfig: CollageConfig = {
|
2021-09-15 21:15:41 +02:00
|
|
|
numImages: args["n"],
|
|
|
|
};
|
2021-09-20 22:38:04 +02:00
|
|
|
const modeKey: DisplayCollageModeType = choice(allModeKeys);
|
2021-09-15 19:08:53 +02:00
|
|
|
|
2021-09-20 22:38:04 +02:00
|
|
|
if (modeKey === "recursive") {
|
|
|
|
console.log(
|
2022-01-19 21:26:06 +01:00
|
|
|
`Creating a recursive collage, choosing from ${shuffledFiles.length} files...`
|
2021-09-20 22:38:04 +02:00
|
|
|
);
|
|
|
|
await denoCollageModes.recursiveDraw(context, images, {
|
2021-09-20 22:56:02 +02:00
|
|
|
modes: args["rm"] ? parseCollageModes(args["rm"]) : collageModeType,
|
|
|
|
repeat: args["rr"],
|
|
|
|
level: args["rl"],
|
2021-09-20 22:41:00 +02:00
|
|
|
});
|
2021-09-20 22:38:04 +02:00
|
|
|
} else {
|
|
|
|
const mode = denoCollageModes.modes[modeKey];
|
|
|
|
console.log(
|
2022-01-19 21:26:06 +01:00
|
|
|
`Creating a "${mode.name}" collage, choosing from ${shuffledFiles.length} files...`
|
2021-09-20 22:38:04 +02:00
|
|
|
);
|
2022-01-19 21:26:06 +01:00
|
|
|
const segments = mode.getSegments(context, collageConfig, images);
|
2021-09-20 22:38:04 +02:00
|
|
|
mode.place(context, images, segments);
|
2022-01-19 21:26:06 +01:00
|
|
|
console.log(
|
|
|
|
`Used: ${images
|
|
|
|
.slice(0, segments.length)
|
|
|
|
.map((img) => img.path)
|
|
|
|
.join(", ")}`
|
|
|
|
);
|
2021-09-20 22:38:04 +02:00
|
|
|
}
|
2021-09-15 19:08:53 +02:00
|
|
|
|
2022-01-19 22:01:04 +01:00
|
|
|
console.log(args);
|
|
|
|
|
|
|
|
if (args["quotes"]) {
|
|
|
|
const text = await Deno.readTextFile(args["quotes"]);
|
|
|
|
const lines = text.split("\n");
|
|
|
|
const quote = choice(lines).trim();
|
|
|
|
console.log(quote);
|
|
|
|
denoCollageModes.placeText(context, quote);
|
|
|
|
}
|
|
|
|
|
2021-09-20 22:40:12 +02:00
|
|
|
const output = args["output"];
|
2021-09-15 19:08:53 +02:00
|
|
|
console.log(`Saving to "${output}"...`);
|
2022-01-19 21:26:06 +01:00
|
|
|
await Deno.writeFile(output, canvas.toBuffer());
|