/**
* Background Removal Workflow for ComfyUI
* Uses RMBG-2.0 model for high-quality background removal
*/
function getBackgroundRemovalWorkflow(imagePath) {
// Add [output] annotation for files in the output directory
// ComfyUI uses [output], [input], or [temp] annotations to specify directories
// Note: ComfyUI expects a space before the annotation
let annotatedPath = imagePath;
if (!imagePath.includes('[')) {
// If it's just a filename, assume it's in the output directory
annotatedPath = `${imagePath} [output]`;
}
return {
"1": {
"inputs": {
"image": annotatedPath
},
"class_type": "LoadImage",
"_meta": {
"title": "Load Image"
}
},
"2": {
"inputs": {
"model": "RMBG-2.0",
"image": ["1", 0],
"sensitivity": 0.5,
"process_res": 1024,
"mask_blur": 0,
"mask_offset": 0,
"invert_output": false,
"refine_foreground": false,
"background": "Alpha"
},
"class_type": "RMBG",
"_meta": {
"title": "Remove Background"
}
},
"3": {
"inputs": {
"filename_prefix": "bg_removed",
"images": ["2", 0]
},
"class_type": "SaveImage",
"_meta": {
"title": "Save Image"
}
}
};
}
/**
* Alternative workflow using Image Composite for transparency
*/
function getBackgroundRemovalWithAlphaWorkflow(imagePath, alphaMatting = true) {
// Add [output] annotation for files in the output directory
// ComfyUI uses [output], [input], or [temp] annotations to specify directories
// Note: ComfyUI expects a space before the annotation
let annotatedPath = imagePath;
if (!imagePath.includes('[')) {
// If it's just a filename, assume it's in the output directory
annotatedPath = `${imagePath} [output]`;
}
const workflow = {
"1": {
"inputs": {
"image": annotatedPath
},
"class_type": "LoadImage",
"_meta": {
"title": "Load Image"
}
},
"2": {
"inputs": {
"model": "RMBG-2.0",
"image": ["1", 0],
"sensitivity": 0.5,
"process_res": 1024,
"mask_blur": 0,
"mask_offset": 0,
"invert_output": false,
"refine_foreground": false,
"background": "Alpha"
},
"class_type": "RMBG",
"_meta": {
"title": "Remove Background"
}
}
};
if (alphaMatting) {
// Add alpha matting for better edges
workflow["3"] = {
"inputs": {
"alpha_matting": true,
"alpha_matting_foreground_threshold": 240,
"alpha_matting_background_threshold": 10,
"alpha_matting_erode_size": 5,
"images": ["2", 0],
"mask": ["2", 1]
},
"class_type": "AlphaMatting",
"_meta": {
"title": "Alpha Matting"
}
};
workflow["4"] = {
"inputs": {
"filename_prefix": "bg_removed_alpha",
"images": ["3", 0]
},
"class_type": "SaveImage",
"_meta": {
"title": "Save Image with Alpha"
}
};
} else {
workflow["3"] = {
"inputs": {
"filename_prefix": "bg_removed",
"images": ["2", 0]
},
"class_type": "SaveImage",
"_meta": {
"title": "Save Image"
}
};
}
return workflow;
}
export {
getBackgroundRemovalWorkflow,
getBackgroundRemovalWithAlphaWorkflow
};