Files
SnapOtter/packages/ai/src/noise-removal.ts
T
ashim-hq 9a015c8501 fix: AVIF sidecar crash, edit-metadata silent no-op, passport batch blank images, color-palette hex overflow, OCR log noise
- Convert all AI bridge inputs to PNG before writing to disk so PIL can
  read AVIF/WebP/TIFF (7 bridge files; face-detection and OCR already
  had this pattern)
- Add title/author aliases to edit-metadata schema so common field names
  actually write EXIF tags instead of being silently stripped by Zod
- Port extend/pad crop logic from passport-photo single endpoint to the
  batch pipeline so crop regions extending beyond the image get filled
  with background color instead of producing all-white output
- Clamp quantized color channels to 255 in color-palette to prevent
  Math.round(255/16)*16=256 from producing invalid hex like #100100100
- Compare OCR fallback warning against expected engine name per tier
  instead of comparing engine name against tier name (always mismatch)
2026-04-21 23:54:25 +08:00

55 lines
1.5 KiB
TypeScript

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 NoiseRemovalOptions {
tier?: string;
strength?: number;
detailPreservation?: number;
colorNoise?: number;
format?: string;
quality?: number;
}
export interface NoiseRemovalResult {
buffer: Buffer;
width: number;
height: number;
format: string;
tier: string;
}
export async function noiseRemoval(
inputBuffer: Buffer,
outputDir: string,
options: NoiseRemovalOptions = {},
onProgress?: ProgressCallback,
): Promise<NoiseRemovalResult> {
const inputPath = join(outputDir, "input_denoise.png");
const outputPath = join(outputDir, "output_denoise.png");
const pngBuffer = await sharp(inputBuffer).png().toBuffer();
await writeFile(inputPath, pngBuffer);
const { stdout } = await runPythonWithProgress(
"noise_removal.py",
[inputPath, outputPath, JSON.stringify(options)],
{ onProgress },
);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw new Error(result.error || "Noise removal failed");
}
const actualOutputPath = result.output_path || outputPath;
const buffer = await readFile(actualOutputPath);
return {
buffer,
width: result.width,
height: result.height,
format: result.format ?? "png",
tier: result.tier ?? options.tier ?? "balanced",
};
}