mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat: improve remove background with edge smoothing, color decontamination, output formats
- Expose birefnet-hr-matting in UI (People/Ultra) and fix model defaults (People/Max now uses birefnet-matting for true alpha matting) - Add output format selector (PNG/WebP/AVIF) with lossless alpha support - Add edge smoothing post-processing (Off/Light/Medium/Strong) via morphological mask refinement to reduce gray halo artifacts - Add color decontamination to remove background color spill from semi-transparent edge pixels - Thread new settings through full stack: frontend -> API schema -> Python sidecar -> Sharp effects pipeline - Add i18n keys for all 21 locales - Add unit tests for new option serialization (3 tests) - Add integration tests for new settings validation (4 tests)
This commit is contained in:
@@ -1,5 +1,26 @@
|
||||
import sharp from "sharp";
|
||||
|
||||
export type BgOutputFormat = "png" | "webp" | "avif";
|
||||
|
||||
export const BG_OUTPUT_FORMATS: BgOutputFormat[] = ["png", "webp", "avif"];
|
||||
|
||||
export const BG_FORMAT_CONTENT_TYPES: Record<BgOutputFormat, string> = {
|
||||
png: "image/png",
|
||||
webp: "image/webp",
|
||||
avif: "image/avif",
|
||||
};
|
||||
|
||||
function toOutputFormat(pipeline: sharp.Sharp, format: BgOutputFormat): Promise<Buffer> {
|
||||
switch (format) {
|
||||
case "webp":
|
||||
return pipeline.webp({ lossless: true }).toBuffer();
|
||||
case "avif":
|
||||
return pipeline.avif({ lossless: true }).toBuffer();
|
||||
default:
|
||||
return pipeline.png().toBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Background removal post-processing effects.
|
||||
* All effects use Sharp (libvips) for fast server-side image manipulation.
|
||||
@@ -182,6 +203,7 @@ export async function applyEffects(
|
||||
blurIntensity?: number;
|
||||
shadowEnabled?: boolean;
|
||||
shadowOpacity?: number;
|
||||
outputFormat?: BgOutputFormat;
|
||||
},
|
||||
): Promise<Buffer> {
|
||||
const meta = await sharp(subjectBuffer).metadata();
|
||||
@@ -238,13 +260,12 @@ export async function applyEffects(
|
||||
}
|
||||
// else: transparent - no background layer
|
||||
|
||||
const fmt = settings.outputFormat ?? "png";
|
||||
|
||||
// Step 3: Composite subject onto background
|
||||
if (background) {
|
||||
return sharp(background)
|
||||
.composite([{ input: subject, blend: "over" }])
|
||||
.png()
|
||||
.toBuffer();
|
||||
return toOutputFormat(sharp(background).composite([{ input: subject, blend: "over" }]), fmt);
|
||||
}
|
||||
|
||||
return subject;
|
||||
return toOutputFormat(sharp(subject), fmt);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user