Nodes/opencv-comfyui/OpenCV bitwise_and_0
ComfyUI Node

OpenCV bitwise_and_0

The AND node for images and masks

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV bitwise_and_0
  • src1
  • src2
  • dst
  • mask
  • nparray

bitwise_and_0 is the node that answers "what's in both of these?" For two images or two masks, it keeps only the pixels where both are non-zero - the intersection. If you've ever wanted to cut a subject out of a region that only exists where two masks overlap, or composite through a mask gate, this is the primitive. It's cv2.bitwise_and wrapped as a node in opencv-comfyui, and despite the intimidating name it's a genuinely friendly one: two images in, one image out.

How it works

Every pixel is treated as a bit pattern and AND-ed with the corresponding pixel of the other input, per channel. In practice, on 8-bit images: black (0) AND anything is black; white (255) AND x is x. So AND-ing an image with a black-and-white mask keeps the image where the mask is white and blacks out the rest - which is exactly how you'd cut an object out of a scene using a mask. And AND-ing two masks gives you their overlap, pixel by pixel.

The inputs:

  • src1 / src2 - two NPARRAYs, same size (OpenCV will throw an assertion error if shapes don't match). Both images or both masks.
  • dst (optional) - out-parameter, leave unwired.
  • mask (optional) - a single-channel 8-bit array that restricts where the AND is written. Pixels where the mask is non-zero get the AND result; elsewhere the output keeps src1's value. The mask is the "only do it here" limiter.
  • Output: one nparray, the result.

The ComfyUI mask gotcha

Here's the trap that bites everyone coming from ComfyUI core: native ComfyUI masks are float 0–1, but this pack works in OpenCV nparray land, where images and masks are 8-bit 0–255. If you wire a native mask straight in, values like 0.5 become "kinda on" instead of a clean gate, and the pack won't auto-convert. Feed it properly formed 0–255 arrays (or pass your Comfy mask through a conversion node first) and keep everything in the same scale. This is exactly the kind of subtle mismatch that makes the pack's README say "expect dragons."

Where it fits

The classic ComfyUI workflow use: build a refined region mask by AND-ing a coarse mask with a fine one, then use the result as a compositing gate or an inpainting region. The KB's detailing doc covers the whole mask-acquisition ecosystem; this is the deterministic glue for combining whatever masks you already have. And because it's pure integer math, it's instant and deterministic - no model, no seed.

Install

ComfyUI Manager → search opencv-comfyui, or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
pip install opencv-python-contrib

Restart ComfyUI, no model downloads. The pack's known install trap is a conflicting OpenCV (Cannot import name 'guidedFilter' from 'cv2.ximgproc'); a clean reinstall fixes it. And yes, bitwise_and_1 is this node's identical overload twin - same function, same behavior.

Categoryimage/OpenCV

Inputs (4)

NameTypeDefaultDescription
src1NPARRAY
src2NPARRAY
dstoptNPARRAY
maskoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY