Nodes/opencv-comfyui/OpenCV divide_2
ComfyUI Node

OpenCV divide_2

Invert an image by a constant, cleanly

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV divide_2
  • src2
  • dst
  • nparray
scale
dtype

divide_2 is the scalar overload of OpenCV's divide: cv2.divide(scale, src2, dtype) computes scale / src2 for every pixel. One image in, one image out, and the whole thing is just "take this constant and divide it by each pixel." Which sounds almost too trivial to be a node - until you realize that's exactly the per-pixel inversion operation you keep wanting and don't have.

Where this earns its place: you often need to invert a map, not ratio two images. A mask or a light map that says "0.25 here" needs to become "4.0 here" so that multiplying by it does the opposite of what the original does. divide_2 gives you that with a single scale knob: scale=255 turns a 0–255 image into its byte-level inverse (255/x), scale=1 gives you the straight reciprocal. It's also just the clean way to normalize - dividing every pixel of an image by a constant scale's reciprocal, or building a compensation field from a scalar reference.

The mechanism is the same saturate_cast per-element path as the rest of the divide family - dst = scale / src2, clamped to the output type's range. The relevant gotcha is the zero again: pixels where src2 is 0 divide by zero, and OpenCV's behavior there is type-dependent and not your friend. If your source image contains true zeros and you're doing real math on the result (not just display), threshold the small values or add an epsilon upstream. And remember dtype -1 means "match the inputs," which is the sane default; the operation's output range can surprise you if the input type flips under you.

Inputs: scale (FLOAT, the constant numerator), src2 (NPARRAY, the denominator image), dtype (INT, -1 default). Optional dst is an out-parameter - skip it. Output: one nparray, the scale/src2 result.

The divide family has four nodes because OpenCV overloads it for two forms, each declared for MatLike and UMat: image-over-image (divide_0/divide_1) and scalar-over-image (divide_2/divide_3). divide_3 is this node's identical twin - use either.

Pack-wide plumbing: NPARRAY in and out - Image2Nparray before, Nparrays2Image after, batch_size==1 only.

Install: ships in geroldmeisinger/opencv-comfyui - ComfyUI Manager → search "OpenCV", or git clone https://github.com/geroldmeisinger/opencv-comfyui into custom_nodes, restart, with opencv-contrib-python installed. The pack-wide Cannot import name 'guidedFilter' conflict error from duplicate OpenCV wheels blocks everything until you fix the wheels.

Worth a spot in your mental toolbox: if you ever find yourself reaching for multiply to undo a division, this node is the honest inverse, and it's a millisecond operation - not a job for a model.

Categoryimage/OpenCV

Inputs (4)

NameTypeDefaultDescription
scaleFLOAT
src2NPARRAY
dtypeINT
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY