ComfyUI Node

Subtraction

The difference-mask node hiding in plain sight

By CoiiChan·Created about a year ago·Updated about a year ago· 2
Subtraction
  • imga
  • imgb
  • IMAGE
formularesult = imga - imgb

Ever squinted at two near-identical renders trying to spot what changed? That's the exact problem this node solves, and the name is the whole manual: result = imga - imgb. Subtraction is one of the derived nodes in CoiiChan's ComfyUI-FuncAsTexture-CoiiNode pack - a thin, pre-baked wrapper over the pack's main CustomScript-NumPy node, which runs a multi-line NumPy formula against your images (a concept lifted straight from the Custom node in Unreal Engine's Material Editor). Where the parent node exposes three images and five number inputs, this one trims everything down to two image slots and a single formula box.

What it actually does

Both images come in as float tensors in [B,H,W,C] shape with 0–1 values. The node converts them to NumPy arrays, preloads a namespace with np, the imga/imgb arrays, per-channel copies and a makergb() helper, then exec()s the formula. Assign to result, and that's your output. For Subtraction, the default formula is literally result = imga - imgb - element-wise subtraction of two arrays, which is exactly what it says on the tin.

The inputs that matter

  • imga (optional IMAGE): the main image. Its resolution defines the output size.
  • imgb (optional IMAGE): the image you subtract. If you leave it unplugged it's replaced with a zero-filled array, so imga - 0 is just imga - subtraction is a no-op without a second input.
  • formula (required, multi-line): defaults to result = imga - imgb, and it's fully editable.

The single IMAGE output feeds into a preview, an upscaler, or another FuncAsTexture node. Nothing fancy about the wiring.

Why you'd reach for it

Subtraction is a difference mask generator. Two passes of the same prompt at different seeds, a pre- and post-upscale version, two ControlNet variants - subtract them and the parts that match cancel out to black while the parts that changed light up. That residual image is exactly the region you might want to feed into an inpaint pass or use as a rough attention map. There are dedicated nodes for some of these comparisons, but none of them are built into this pack's scope, and none give you the math underneath.

One honest caveat: because you write the formula yourself, a bare subtraction usually looks terrible on screen. Values below zero render as black, so a normal imga - imgb darkens the unchanged areas to void and the "what changed" signal is half-negative. The fix is a one-line tweak in the formula box:

result = np.clip(imga - imgb, 0, 1)          # keep it displayable
result = (imga - imgb + 1) * 0.5             # center zero at mid-gray

The second version is the one you'll actually want for eyeballing differences - unchanged areas sit at neutral gray, differences go brighter or darker depending on direction.

Installing it

Same story as the rest of the pack: no requirements.txt (NumPy, PyTorch and PIL are already in ComfyUI), no model files to fetch. ComfyUI Manager: search ComfyUI-FuncAsTexture-CoiiNode. Or manually:

cd ComfyUI/custom_nodes
git clone https://github.com/CoiiChan/ComfyUI-FuncAsTexture-CoiiNode

Restart, and Subtraction lives under the FunctionAsTexture category.

Where you'll trip

Resolution mismatch is the big one. The README is blunt about it: when you calculate with multiple input images, their resolutions need to match or you get a broadcasting error. And imga sets the output size - if you leave it unplugged the node falls back to 512×512, which almost certainly won't match imgb, and subtraction blows up. So always feed imga, and resize imgb to match first. Also remember the no-result trap shared by every node in this pack: forget to assign result and the node quietly hands your input back unchanged, which is baffling until you know it's a feature.

CategoryFunctionAsTexture

Inputs (3)

NameTypeDefaultDescription
formulaSTRINGresult = imga - imgb
imgaoptIMAGEOptional ,Reference size ,Default =(1,512,512,3)
imgboptIMAGEOptional

Outputs (1)

NameTypeDescription
IMAGEIMAGE