Files
SnapOtter/packages/ai/src/upscaling.ts
T
Siddharth Kumar Sah fe376aebd2 feat: overhaul upscale with bug fixes and advanced features
- Fix multi-image: process selected file, not always first
- Fix progress bar: asymptotic fill prevents visual stalling
- Fix slider: write results to captured index, not current selection
- Add model selection (Auto/AI/Fast), face enhancement, denoise
- Add output format (PNG/JPEG/WebP) with quality control
- Add Upscale All for sequential batch processing with queue
- More granular Python progress stages for smoother UX
2026-04-12 19:04:23 +08:00

54 lines
1.4 KiB
TypeScript

import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { type ProgressCallback, runPythonWithProgress } from "./bridge.js";
export interface UpscaleOptions {
scale?: number;
model?: string;
faceEnhance?: boolean;
denoise?: number;
format?: string;
quality?: number;
}
export interface UpscaleResult {
buffer: Buffer;
width: number;
height: number;
method: string;
format: string;
}
export async function upscale(
inputBuffer: Buffer,
outputDir: string,
options: UpscaleOptions = {},
onProgress?: ProgressCallback,
): Promise<UpscaleResult> {
const inputPath = join(outputDir, "input_upscale.png");
const outputPath = join(outputDir, "output_upscale.png");
await writeFile(inputPath, inputBuffer);
const { stdout } = await runPythonWithProgress(
"upscale.py",
[inputPath, outputPath, JSON.stringify(options)],
{ onProgress },
);
const result = JSON.parse(stdout);
if (!result.success) {
throw new Error(result.error || "Upscaling failed");
}
// Python may write to a different path when the output format changes
const actualOutputPath = result.output_path || outputPath;
const buffer = await readFile(actualOutputPath);
return {
buffer,
width: result.width,
height: result.height,
method: result.method ?? "unknown",
format: result.format ?? "png",
};
}