ComfyUI Node

Distance

How different are these two images? The Euclidean distance node

By CoiiChan·Created about a year ago·Updated about a year ago· 2
Distance
  • imga
  • imgb
  • IMAGE
formularesult = np.sqrt(np.sum((imga - imgb) ** 2, axis=-1))

Distance answers a question that comes up more than you'd think in image workflows: at every pixel, how different are these two images? The formula is textbook Euclidean distance - result = np.sqrt(np.sum((imga - imgb) ** 2, axis=-1)) - which means: take the channel-wise differences, square them, add them across the three color channels, square-root. One number per pixel, telling you how far apart the two colors are. Bright pixels = big difference; black pixels = identical.

That makes it a difference map, and difference maps are quietly one of the most useful things in image processing:

  • Change detection. Compare a source image to a processed version; the bright spots are where the processing actually altered things, and where it didn't. Great for sanity-checking a filter.
  • Mask generation from reference color. Diff an image against a solid color (the pack's Contant3Vector) and you get a mask that's bright exactly where the image isn't that color - an inverted chroma-style key with zero tuning.
  • Error/quality checks. Diff a render against its ground truth and you can literally see the error distribution.

How it works

The axis=-1 collapses RGB into a single per-pixel distance, so like Dot, the output is grayscale data that gets expanded back to three channels. The **2 and np.sqrt are what make it "Euclidean" - no absolute values, no np.linalg.norm needed. Everything runs in the same pre-filled-formula framework as the rest of the pack's basic nodes.

Inputs and outputs

  • formula - the distance expression, pre-filled and editable.
  • imga - reference image, sets output resolution.
  • imgb - second image. Unconnected = zeros, so you get distance-from-black, which is just per-pixel brightness magnitude. That's actually a useful mode: it gives you a luminance map with a pleasant square-root response.

One IMAGE output.

Install

ComfyUI Manager → search "FuncAsTexture", or:

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

No models, no requirements.txt. Category: FunctionAsTexture.

Gotchas

  • Max distance is √3 ≈ 1.73, not 1 - three channels each differing by up to 1.0. So "all black = identical, all white = maximally different" isn't quite true; the ceiling is a bit higher, and clamping to 1.0 will crush only the most extreme differences. Fine in practice, worth knowing if you're thresholding.
  • Same-resolution rule - mismatched imga/imgb throw a shape error, like every two-image node here.
  • It's a difference magnitude. Direction is lost - a pixel where image A is bright and image B is dark gives the same distance as the reverse. If you need to know which way colors shifted, you want imga - imgb instead (the pack's Subtraction-style formula in CustomScript-NumPy).

Distance and Dot are the pack's two "compare" nodes - Dot scores agreement, Distance scores difference. For "did my filter change anything and where," Distance is the one you'll actually use.

CategoryFunctionAsTexture

Inputs (3)

NameTypeDefaultDescription
formulaSTRINGresult = np.sqrt(np.sum((imga - imgb) ** 2, axis=-1))
imgaoptIMAGEOptional ,Reference size ,Default =(1,512,512,3)
imgboptIMAGEOptional

Outputs (1)

NameTypeDescription
IMAGEIMAGE