Nodes/ComfyUI_UnfakePy_Warpper/Remove Background (FloodFill)
ComfyUI Node

Remove Background (FloodFill)

Cut out your sprite with a flood fill — no model, no downloads

By AhBumm·Created 11 months ago·Updated 9 months ago· 2
Remove Background (FloodFill)
  • image
  • IMAGE
  • MASK
  • MASK
min_hole_ratio0.18

Every pixel-art workflow eventually asks the same question: how do I get the background off this sprite? And the standard answer - a segmentation model like BiRefNet - is honestly overkill for flat-color pixel art, and it can be worse than the cheap trick. This node uses the cheap trick, done well: a classic flood fill. It figures out the background color from the image's edges, pours a "fill" inward from the borders, and everything the fill touches becomes transparent. No model download, no VRAM, runs on CPU in well under a second, and - crucially for pixel art - it keeps the hard block edges intact, which is exactly where neural background-removers tend to chew them up.

How it works

The implementation in rmbg_tools.py is more careful than a naive flood fill, and reading it tells you what to expect. It converts the image to Lab color space (which handles color distance far better than RGB) and samples a 3-pixel border to estimate the background color - using the median, so a few stray edge pixels don't poison the estimate. Then it requires the border to be at least 42% consistent with that color; if the border is busy or textured, it bails out and returns the image unchanged rather than cutting a garbage hole. If the border passes, it runs an adaptive flood fill with up to three attempts, adjusting the color threshold up or down depending on whether it's eating too much or too little. There's a min_hole_ratio sanity check so holes inside the subject only get removed as background when they're plausibly part of the background, and finally it drops floating specks smaller than a cutoff size. All of it is printed to the console as it runs - [estimate_bg_color], [adaptive_flood], [quality_check] lines - which is genuinely useful when a cutout comes out wrong.

Inputs and outputs

  • image - the sprite to cut out.
  • min_hole_ratio (FLOAT, default 0.18, range 0–1) - the threshold that decides whether internal holes are background. Higher = more aggressive at filling holes inside the subject; lower = preserves more interior detail. The default is a sensible middle.

Three outputs come back: an IMAGE - the cutout as an RGBA image with the background as transparency - and two MASK outputs: the foreground mask (the subject's alpha) and a background mask (inverted). Both masks are useful: wire the foreground mask into compositing or inpainting, or use the background mask to, say, refill the background with a different color without touching the sprite.

Installing it

cd ComfyUI/custom_nodes
git clone https://github.com/AhBumm/ComfyUI_UnfakePy_Warpper

or ComfyUI Manager → search ComfyUI_UnfakePy_Warpper, restart. Deps: unfake==1.0.7 + nest_asyncio - the flood-fill code uses OpenCV, which comes in transitively with unfake. Python 3.10+, no models.

Where it wins and where it loses

This is the right tool when the background is a uniform color touching the image borders - the classic sprite case. It's the wrong tool for photos, gradients, or anything with a busy backdrop; those will fall back to the unchanged image (watch the console for Edge colors not uniform, skipping), and for that job you genuinely want BiRefNet or rembg. The overlap worth knowing: for clean flat-color art this often beats the neural models outright on edge quality, because a segmentation network can't help but soften the boundary it draws, while a flood fill produces a mathematically exact edge. It's a different philosophy, and for pixel art the exact edge usually wins.

CategoryBillbum/PixelTools

Inputs (2)

NameTypeDefaultDescription
imageIMAGE
min_hole_ratioFLOAT0.180–1

Outputs (3)

NameTypeDescription
IMAGEIMAGE
MASKMASK
MASKMASK