reformat cli/main.ts
This commit is contained in:
parent
14efcf03a1
commit
a656d37d8c
1 changed files with 29 additions and 13 deletions
42
cli/main.ts
42
cli/main.ts
|
@ -1,21 +1,31 @@
|
||||||
import { parse } from "https://deno.land/std@0.106.0/flags/mod.ts";
|
import { parse } from "https://deno.land/std@0.106.0/flags/mod.ts";
|
||||||
import { createCanvas, loadImage } from "https://deno.land/x/canvas/mod.ts";
|
import { createCanvas, loadImage } from "https://deno.land/x/canvas/mod.ts";
|
||||||
import { CollageModes, CollageModeType, collageModeType } from "../src/common/collages.ts";
|
import {
|
||||||
import DenoCollageModes, { CastCanvasRenderingContext, ProxyImage } from "./collages.ts";
|
CollageModes,
|
||||||
|
CollageModeType,
|
||||||
|
collageModeType,
|
||||||
|
} from "../src/common/collages.ts";
|
||||||
|
import DenoCollageModes, {
|
||||||
|
CastCanvasRenderingContext,
|
||||||
|
ProxyImage,
|
||||||
|
} from "./collages.ts";
|
||||||
import { CollageConfig } from "../src/common/types.ts";
|
import { CollageConfig } from "../src/common/types.ts";
|
||||||
import { choice, shuffle } from "../src/common/utils.ts";
|
import { choice, shuffle } from "../src/common/utils.ts";
|
||||||
|
|
||||||
const args = parse(Deno.args, {
|
const args = parse(Deno.args, {
|
||||||
alias: {
|
alias: {
|
||||||
'o': 'output'
|
"o": "output",
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const images: ProxyImage[] = (await Promise.all(args["_"].map((imageURL) => loadImage(imageURL.toString())))).map((image) => new ProxyImage(image));
|
const images: ProxyImage[] =
|
||||||
|
(await Promise.all(
|
||||||
|
args["_"].map((imageURL) => loadImage(imageURL.toString())),
|
||||||
|
)).map((image) => new ProxyImage(image));
|
||||||
|
|
||||||
if (images.length < 2) {
|
if (images.length < 2) {
|
||||||
console.error("kollagen needs at least 2 images to work.");
|
console.error("kollagen needs at least 2 images to work.");
|
||||||
Deno.exit(1);
|
Deno.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const modes = new DenoCollageModes();
|
const modes = new DenoCollageModes();
|
||||||
|
@ -23,20 +33,26 @@ const modes = new DenoCollageModes();
|
||||||
const modeKey: CollageModeType = args["mode"] || choice(collageModeType);
|
const modeKey: CollageModeType = args["mode"] || choice(collageModeType);
|
||||||
const mode = modes.modes[modeKey];
|
const mode = modes.modes[modeKey];
|
||||||
|
|
||||||
console.log(`Creating a "${mode.name}" collage from ${images.length} images...`);
|
console.log(
|
||||||
|
`Creating a "${mode.name}" collage from ${images.length} images...`,
|
||||||
|
);
|
||||||
console.debug(`Images: ${args["_"].join(", ")}`);
|
console.debug(`Images: ${args["_"].join(", ")}`);
|
||||||
|
|
||||||
const canvas = createCanvas(args["width"] || 640, args["height"] || 640);
|
const canvas = createCanvas(args["width"] || 640, args["height"] || 640);
|
||||||
const context = canvas.getContext("2d");
|
const context = canvas.getContext("2d");
|
||||||
|
|
||||||
const collageConfig: CollageConfig = {
|
const collageConfig: CollageConfig = {
|
||||||
numImages: args["n"]
|
numImages: args["n"],
|
||||||
}
|
};
|
||||||
|
|
||||||
const shuffledImages = shuffle(images);
|
const shuffledImages = shuffle(images);
|
||||||
const segments = mode.getSegments(context as CastCanvasRenderingContext, collageConfig, shuffledImages);
|
const segments = mode.getSegments(
|
||||||
|
context as CastCanvasRenderingContext,
|
||||||
|
collageConfig,
|
||||||
|
shuffledImages,
|
||||||
|
);
|
||||||
mode.place(context as CastCanvasRenderingContext, shuffledImages, segments);
|
mode.place(context as CastCanvasRenderingContext, shuffledImages, segments);
|
||||||
|
|
||||||
const output = args["output"] || "collage.png";
|
const output = args["output"] || "collage.png";
|
||||||
console.log(`Saving to "${output}"...`);
|
console.log(`Saving to "${output}"...`);
|
||||||
await Deno.writeFile(output, canvas.toBuffer());
|
await Deno.writeFile(output, canvas.toBuffer());
|
||||||
|
|
Loading…
Reference in a new issue