image math
Add, subtract, multiply, or AND two images or masks
- image1
- image2
- mask1
- mask2
- IMAGE
- MASK
image math does exactly what it sounds like: it takes two images (or two masks, or one of each) and combines them with basic pixel arithmetic - add, subtract, multiply, or a bitwise AND. If you've ever wanted a difference mask between two renders, wanted to multiply a mask onto an image to punch a hole in it, or needed to AND two masks together to keep only their overlap, this is the node for that.
The inputs and outputs that matter
operation is a four-way pick: - (subtract, the default), + (add), * (multiply), and & (bitwise AND - effectively an intersection). algorithm chooses the backend doing the math: cv2 (OpenCV, the default) or torch. Both should give the same result for these operations; cv2 tends to be faster on CPU, torch keeps everything as tensors and stays on GPU if your images are already there. invert1 and invert2 flip either input before the operation runs - useful for subtracting the inverse of a mask rather than the mask itself, without an extra Invert node cluttering your graph.
Inputs are all optional at the schema level - image1, image2, mask1, mask2 - because the node handles either an image pair or a mask pair (or a mix) depending on what you actually wire in. Outputs mirror that: an IMAGE and a MASK, and whichever path you fed data into is the one that comes back populated.
Worth flagging: the pack's own blurb for this node mentions "expression support," but the node's actual inputs are a fixed operation picklist, not a text field for formulas. If you want arbitrary math expressions on images, that's any_math and any_math_v2 elsewhere in this pack - this node is the simpler, faster, pick-an-operation version.
Installing it
Grab the whole pack - there's no way to install individual nodes out of it. ComfyUI Manager: search "ComfyUI-WJNodes," install, restart. By hand:
cd ComfyUI/custom_nodes
git clone https://github.com/807502278/ComfyUI-WJNodes.git
Restart afterward. No model downloads for this node, and per the author most dependencies already ship with a normal ComfyUI install - you shouldn't need the optional pip install -r requirements.txt step just for image math.
Where people get burned
The cv2 backend needs OpenCV's Python bindings (opencv-python). It's an extremely common package - most ComfyUI installs already have it pulled in by some other custom node - but if you're on a minimal or fresh environment and this node throws an import error on cv2, that's the first thing to check; switching algorithm to torch sidesteps the dependency entirely. (The pack's mask-only sibling, mask_and_mask_math, documents an explicit auto-fallback to torch when cv2 isn't found - the same cv2/torch choice shows up here, so keep that in mind if this node behaves the same way.) Also keep your two inputs at matching resolution and channel count going in - pixel-wise math across mismatched shapes is the classic way to get a broadcast error instead of the blend you were expecting.
Inputs (8)
| Name | Type | Default | Description |
|---|---|---|---|
| operation | COMBO | - | 4 options: -, +, *, & |
| algorithm | COMBO | cv2 | 2 options: cv2, torch |
| invert1 | BOOLEAN | false | — |
| invert2 | BOOLEAN | false | — |
| image1opt | IMAGE | — | |
| image2opt | IMAGE | — | |
| mask1opt | MASK | — | |
| mask2opt | MASK | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |
| MASK | MASK | — |