2021-09-15 19:08:53 +02:00
|
|
|
import { parse } from "https://deno.land/std@0.106.0/flags/mod.ts";
|
2021-09-15 21:16:01 +02:00
|
|
|
import { createCanvas, init } from "https://deno.land/x/canvas/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";
|
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-15 21:16:01 +02:00
|
|
|
const canvasKit = await init();
|
|
|
|
|
2021-09-15 19:08:53 +02:00
|
|
|
const args = parse(Deno.args, {
|
2021-09-15 21:15:41 +02:00
|
|
|
alias: {
|
2021-09-15 21:16:01 +02:00
|
|
|
"w": "width",
|
|
|
|
"h": "height",
|
2021-09-15 21:15:41 +02:00
|
|
|
"o": "output",
|
2021-09-15 21:16:01 +02:00
|
|
|
"m": "mode",
|
2021-09-15 21:15:41 +02:00
|
|
|
},
|
2021-09-15 19:08:53 +02:00
|
|
|
});
|
|
|
|
|
2021-09-17 18:56:57 +02:00
|
|
|
const files: string[] = [];
|
|
|
|
|
|
|
|
args["_"].forEach((arg) => {
|
|
|
|
arg = arg.toString();
|
|
|
|
if (Deno.statSync(arg).isDirectory) {
|
|
|
|
Array.from(Deno.readDirSync(arg)).forEach((entry) =>
|
|
|
|
files.push(`${arg}/${entry.name}`)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
files.push(arg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (files.length < 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-15 21:16:01 +02:00
|
|
|
const images: ProxyImage[] = (await Promise.all(
|
2021-09-17 18:56:57 +02:00
|
|
|
files.map(async (imageURL) =>
|
|
|
|
canvasKit.MakeImageFromEncoded(await Deno.readFile(imageURL))
|
2021-09-15 21:16:01 +02:00
|
|
|
),
|
|
|
|
)).map((image) => new ProxyImage(image!));
|
|
|
|
|
2021-09-15 19:08:53 +02:00
|
|
|
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 from ${images.length} images...`,
|
|
|
|
);
|
2021-09-17 18:56:57 +02:00
|
|
|
console.debug(`Images: ${files.join(", ")}`);
|
2021-09-15 19:08:53 +02:00
|
|
|
|
|
|
|
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 19:08:53 +02:00
|
|
|
|
|
|
|
const shuffledImages = shuffle(images);
|
2021-09-15 21:15:41 +02:00
|
|
|
const segments = mode.getSegments(
|
|
|
|
context as CastCanvasRenderingContext,
|
|
|
|
collageConfig,
|
|
|
|
shuffledImages,
|
|
|
|
);
|
2021-09-15 19:08:53 +02:00
|
|
|
mode.place(context as CastCanvasRenderingContext, shuffledImages, segments);
|
|
|
|
|
2021-09-15 20:56:40 +02:00
|
|
|
const output = args["output"] || "collage.png";
|
2021-09-15 19:08:53 +02:00
|
|
|
console.log(`Saving to "${output}"...`);
|
2021-09-15 21:15:41 +02:00
|
|
|
await Deno.writeFile(output, canvas.toBuffer());
|