IdentifyBorderColorToMask-badger
Mask the matte frame around your image — flood-fill from the edges
- image
- IMAGE
- MASK
You've got an image with a frame you don't want - white matte bars, a black letterbox band, a solid-color border around a transparent PNG. You want a mask of just that border, so you can inpaint it away or keep it protected. IdentifyBorderColorToMask-badger is the node that builds that mask, and the smart part is how: instead of matching every pixel of that color in the whole image, it flood-fills from the edges outward, so it only catches the border region that's actually connected to the edge. Same color in the middle of the image? Left alone.
That distinction matters for inpainting. The KB's inpainting playbook says masks are still the way to edit only a region while keeping the rest bit-identical - and a border mask is the textbook case: you want to regenerate just the letterbox bars, not every similar-colored pixel elsewhere in the scene. IdentifyColorToMask-badger (the sibling node in this pack) matches color anywhere; this one scopes it to the connected edge region, which is exactly what a frame actually is.
How it works
It starts a breadth-first search from every edge pixel. A pixel joins the mask if it's non-transparent and its color is within detection_threshold of your target color; matching pixels get their neighbors added to the queue. Because the search starts at the border and only spreads through similar-colored pixels, the result is the connected border region - including the bits of it that are technically a slightly different shade, as long as they're within tolerance of your target and connected through matching pixels.
- color (STRING, default
#ffffff) - the hex color of the border you're targeting. - detection_threshold (INT, default 5) - how close a pixel's color must be to count. It's a Euclidean RGB distance; 5 is tight (good for clean solid borders), 30–50 is loose (for gradients or anti-aliased edges). Raise it if the border keeps getting holes.
Inputs and outputs
- image - the IMAGE.
- color - the target border color as hex.
- detection_threshold - the matching tolerance.
Two outputs, and they're the same mask in two formats:
- An IMAGE - black background, white where the border was. Good for previewing.
- A MASK - the same region as a proper mask tensor, ready to wire into an inpainting sampler,
ApplyMaskToImage-badger, or a compositor.
Installing it
In ComfyUI_BadgerTools: ComfyUI Manager → search "BadgerTools" → install, or
cd ComfyUI/custom_nodes
git clone https://github.com/AbyssYuan0/ComfyUI_BadgerTools
Restart after install. No models. Pack tax applies: heavy requirements (moviepy, open_clip_torch, scikit-image, etc.) import at startup even for this Pillow-only node.
Gotchas
- It must be the edge color. The flood-fill starts at the border and never reaches the middle - if your target color also appears inside the subject, this node correctly ignores it. If you actually want all matching pixels, use
IdentifyColorToMask-badgerinstead. - Threshold is the dial that makes or breaks it. Too low and the mask has holes where anti-aliasing shifted the shade; too high and the border search bleeds into the image. Start at 5, bump by 10s, watch the IMAGE preview.
- Transparent edge pixels stop the spread - the code skips pixels with alpha 0, so a semi-transparent frame won't flood properly.
No community threads exist for this node - it's a personal-pack utility. But "mask the connected border, leave the rest alone" is a genuinely smart primitive for cleaning letterboxed and matted images, and this is the cleanest small implementation of it you'll find.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| color | STRING | #ffffff | — |
| detection_threshold | INT | 51–4096 | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |
| MASK | MASK | — |