Nodes/UIIIAIII Toolkit/Background Fill
ComfyUI Node

Background Fill

Paste a subject onto a canvas, no API involved

By uiiiaiii·Created 23 days ago·Updated 10 days ago· 0
Background Fill
  • image
  • background
  • mask
  • image
  • mask
◄background_width1024►
◄background_height1024►
◄foreground_width512►
◄foreground_height512►
◄background_color#000000►
◄feathering0►

After three API nodes in this pack, here's the one that runs entirely on your machine and does something boring but genuinely useful: scale a foreground image to a size you choose, centre it on a canvas, and fill the rest with a solid colour or a background picture. Optional edge feathering. No key, no network, no queue.

Where you'd reach for it: a cut-out subject that needs padding to a square for a LoRA dataset or a thumbnail; a product shot that needs to be dropped on a brand colour; a subject on a clean canvas before it goes back into an inpaint pass. It's the compositing step people usually do in Photoshop because the alternative is five nodes - resize, canvas, paste, blur - and this is one node that does all five.

The inputs

Required:

  • image - the foreground. This is what gets placed.
  • background_width / background_height - canvas size, default 1024×1024.
  • foreground_width / foreground_height - the size the subject is scaled to, default 512×512. Scaling is proportional, so the subject keeps its shape and fits inside that box; it doesn't stretch to fill it.
  • background_color - #RRGGBB, default black. Only used when nothing is wired into background.
  • feathering - in pixels, default 0. Above zero, the foreground's edge dissolves into the background over that many pixels.

Optional:

  • background - an IMAGE to use as the backdrop. Plug one in and background_color is ignored.
  • mask - a MASK that replaces the mask this node outputs.

Outputs are two: image (the composite) and mask (a mask matching the canvas - 1 inside the pasted region, 0 outside, with a soft gradient if you feathered). That mask output is the reason this node is more than a paste: it's the "where did the subject go" map, ready for an inpaint or a blend downstream.

Two behaviours that will confuse you if nobody says them out loud

The mask input doesn't cut out the foreground. Wiring a mask in changes the mask the node returns; it doesn't make the pasted region mask-shaped. If you want a soft, irregular cutout, do that upstream - segment, apply the mask to the image's alpha, and composite those. What you'd use the mask input for is correcting the emitted mask when your subject isn't a centred rectangle: you feed the real shape, the downstream inpaint gets the right region.

Alpha gets dropped. The foreground is converted to RGB before scaling, which means a transparent PNG arrives as a flattened image. If you came here hoping to preserve a cutout's alpha, this node isn't that - it can't distinguish "transparent background" from "black background" after conversion. Do your compositing with the alpha upstream (see the KB's background-removal notes on BiRefNet matting for real fractional alpha, which is the harder case).

Two more small ones: the foreground is centred, always - there's no offset control, so for off-centre framing you'd composite a wider foreground. And only the first frame of a batch is used; feed a batch and you get the same composite repeated.

How it works

Straight tensor math. The foreground gets a bilinear resize to your target box, the background is either resized to the canvas or generated as a flat colour from the hex string, and the foreground is written in at the centred offset. Feathering builds an alpha map, blurs it with a box kernel sized to the feathering value, and blends the foreground against the background by that alpha - so "feathering" here is a soft edge, not a cutout.

One hard rule: if the foreground ends up larger than the canvas, the node raises an error - the message text is in Chinese (前景...大于背景画布...), since that's the pack's development language. The fix is obvious: shrink the foreground numbers or grow the canvas.

Install

Nothing special. It's a local node, so it needs no API key at all - you don't have to configure Agnes or ModelScope to use it, which is a nice thing to know given how much of this pack does.

cd ComfyUI/custom_nodes
git clone https://github.com/uiiiaiii/UIIIAIII_Toolkit.git
# restart ComfyUI

Or search UIIIAIII Toolkit in ComfyUI Manager. The pack's only dependency is requests, and this node doesn't even need that.

When it fights you

The error message is in Chinese. Now you know what it says: foreground bigger than canvas. Background dimensions also step in multiples of 8, both bounded at 16384.

Black edges around a cutout. You feathered too far, or your cutout's alpha was dropped on the way in. Feathering blends toward the background colour, so a wide feather on a hair edge against white will produce a grey halo. Keep feathering small - 2 to 8 px is usually enough to kill a hard staircase - and check KB's post-processing notes if the seam is really a colour mismatch rather than an alpha problem: a colour-match pass does more for "it doesn't sit in the scene" than more feathering ever will.

Spot colour doesn't match. background_color takes #RRGGBB (or shorthand #RGB); anything malformed falls back to black without warning. That's why your "brand blue" came out black.

CategoryUIIIAIII Toolkit/Tools

Inputs (9)

NameTypeDefaultDescription
imageIMAGE—
background_widthINT102416–16384—
background_heightINT102416–16384—
foreground_widthINT51216–16384—
foreground_heightINT51216–16384—
background_colorSTRING#000000Solid fill color when no background image is connected (#RRGGBB)
featheringINT00–4096—
backgroundoptIMAGE—
maskoptMASK—

Outputs (2)

NameTypeDescription
imageIMAGE—
maskMASK—