mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat: add Node bridge for outpainting
This commit is contained in:
@@ -15,6 +15,8 @@ export { detectFaceLandmarks } from "./face-landmarks.js";
|
||||
export { inpaint } from "./inpainting.js";
|
||||
export { noiseRemoval } from "./noise-removal.js";
|
||||
export { extractText } from "./ocr.js";
|
||||
export type { OutpaintOptions } from "./outpainting.js";
|
||||
export { outpaint } from "./outpainting.js";
|
||||
export { removeRedEye } from "./red-eye-removal.js";
|
||||
export { restorePhoto } from "./restoration.js";
|
||||
export { seamCarve } from "./seam-carving.js";
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import sharp from "sharp";
|
||||
import { type ProgressCallback, parseStdoutJson, runPythonWithProgress } from "./bridge.js";
|
||||
|
||||
export interface OutpaintOptions {
|
||||
extendTop: number;
|
||||
extendRight: number;
|
||||
extendBottom: number;
|
||||
extendLeft: number;
|
||||
}
|
||||
|
||||
export async function outpaint(
|
||||
inputBuffer: Buffer,
|
||||
options: OutpaintOptions,
|
||||
outputDir: string,
|
||||
onProgress?: ProgressCallback,
|
||||
): Promise<Buffer> {
|
||||
const inputPath = join(outputDir, "input_outpaint.png");
|
||||
const outputPath = join(outputDir, "output_outpaint.png");
|
||||
|
||||
const pngInput = await sharp(inputBuffer).png().toBuffer();
|
||||
await writeFile(inputPath, pngInput);
|
||||
|
||||
const { stdout } = await runPythonWithProgress(
|
||||
"outpaint.py",
|
||||
[
|
||||
inputPath,
|
||||
outputPath,
|
||||
String(options.extendTop),
|
||||
String(options.extendRight),
|
||||
String(options.extendBottom),
|
||||
String(options.extendLeft),
|
||||
],
|
||||
{ onProgress },
|
||||
);
|
||||
|
||||
const result = parseStdoutJson(stdout);
|
||||
if (!result.success) {
|
||||
throw new Error(result.error || "Outpainting failed");
|
||||
}
|
||||
|
||||
return readFile(outputPath);
|
||||
}
|
||||
Reference in New Issue
Block a user