60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import { parse } from "https://deno.land/std@0.106.0/flags/mod.ts";
|
|
import { createCanvas, init } from "https://deno.land/x/canvas/mod.ts";
|
|
import { CollageModeType, collageModeType } from "../src/common/collages.ts";
|
|
import DenoCollageModes, {
|
|
CastCanvasRenderingContext,
|
|
ProxyImage,
|
|
} from "./collages.ts";
|
|
import { CollageConfig } from "../src/common/types.ts";
|
|
import { choice, shuffle } from "../src/common/utils.ts";
|
|
|
|
const canvasKit = await init();
|
|
|
|
const args = parse(Deno.args, {
|
|
alias: {
|
|
"w": "width",
|
|
"h": "height",
|
|
"o": "output",
|
|
"m": "mode",
|
|
},
|
|
});
|
|
|
|
if (args["_"].length < 2) {
|
|
console.error("kollagen needs at least 2 images to work.");
|
|
Deno.exit(1);
|
|
}
|
|
|
|
const images: ProxyImage[] = (await Promise.all(
|
|
args["_"].map(async (imageURL) =>
|
|
canvasKit.MakeImageFromEncoded(await Deno.readFile(imageURL.toString()))
|
|
),
|
|
)).map((image) => new ProxyImage(image!));
|
|
|
|
const modes = new DenoCollageModes();
|
|
|
|
const modeKey: CollageModeType = args["mode"] || choice(collageModeType);
|
|
const mode = modes.modes[modeKey];
|
|
|
|
console.log(
|
|
`Creating a "${mode.name}" collage from ${images.length} images...`,
|
|
);
|
|
console.debug(`Images: ${args["_"].join(", ")}`);
|
|
|
|
const canvas = createCanvas(args["width"] || 640, args["height"] || 640);
|
|
const context = canvas.getContext("2d");
|
|
|
|
const collageConfig: CollageConfig = {
|
|
numImages: args["n"],
|
|
};
|
|
|
|
const shuffledImages = shuffle(images);
|
|
const segments = mode.getSegments(
|
|
context as CastCanvasRenderingContext,
|
|
collageConfig,
|
|
shuffledImages,
|
|
);
|
|
mode.place(context as CastCanvasRenderingContext, shuffledImages, segments);
|
|
|
|
const output = args["output"] || "collage.png";
|
|
console.log(`Saving to "${output}"...`);
|
|
await Deno.writeFile(output, canvas.toBuffer());
|