import { removeBackground, segmentForeground, applySegmentationMask } from '@imgly/background-removal-node'; import { nanoid } from 'nanoid'; import fs from 'node:fs'; export async function run(imageFilePath: string): Promise { console.log(`imageFilePath=${imageFilePath}`); const config = { debug: false, // publicPath: ..., progress: (key, current, total) => { const [type, subtype] = key.split(':'); console.log( `progress:: ${type} ${subtype} ${((current / total) * 100).toFixed(0)}%` ); }, model: 'small', output: { quality: 0.8, format: 'image/webp' //image/jpeg, image/webp } }; console.time(); const blob = await removeBackground(imageFilePath, config); // const mask = await segmentForeground(randomImage, config); // const blob = await applySegmentationMask(randomImage, mask, config); console.timeEnd(); const buffer = await blob.arrayBuffer(); const format = config.output.format.split('/').pop(); const outFile = `tmp/${nanoid()}.${format}`; await fs.promises.mkdir('tmp', { recursive: true }); await fs.promises.writeFile(outFile, Buffer.from(buffer)); console.log(`Image saved to ${outFile}`); return outFile }