Files
SnapOtter/packages/ai/src/background-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

49 lines
1.6 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { readFile, unlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import sharp from "sharp";
import { type ProgressCallback, parseStdoutJson, runPythonWithProgress } from "./bridge.js";
export interface RemoveBackgroundOptions {
model?: string;
backgroundColor?: string;
}
export async function removeBackground(
inputBuffer: Buffer,
outputDir: string,
options: RemoveBackgroundOptions = {},
onProgress?: ProgressCallback,
): Promise<Buffer> {
const id = randomUUID();
const inputPath = join(tmpdir(), `rembg_in_${id}.png`);
const outputPath = join(outputDir, `rembg_out_${id}.png`);
const pngBuffer = await sharp(inputBuffer).png().toBuffer();
await writeFile(inputPath, pngBuffer);
try {
const meta = await sharp(inputBuffer).metadata();
const megapixels = ((meta.width ?? 0) * (meta.height ?? 0)) / 1_000_000;
const baseTimeout = options.model?.startsWith("birefnet") ? 600000 : 300000;
const timeout = Math.max(baseTimeout, megapixels * 30 * 1000);
const { stdout } = await runPythonWithProgress(
"remove_bg.py",
[inputPath, outputPath, JSON.stringify(options)],
{ onProgress, timeout },
);
const result = parseStdoutJson(stdout);
if (!result.success) {
throw new Error(result.error || "Background removal failed");
}
const outputBuffer = await readFile(outputPath);
return outputBuffer;
} finally {
// Clean up temp files
await unlink(inputPath).catch(() => {});
await unlink(outputPath).catch(() => {});
}
}