Nodes/opencv-comfyui/OpenCV cvtColor_0
ComfyUI Node

OpenCV cvtColor_0

The Node That Makes This Pack Usable (BGR, Gray, and Everything Between)

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV cvtColor_0
  • src
  • dst
  • nparray
code
dstCn
hint

If you use exactly one node from geroldmeisinger/opencv-comfyui, it's this one. cvtColor is OpenCV's color-space converter - BGR to grayscale, RGB to BGR, BGR to HSV, gray back to BGR - and it's the glue that makes every other node in this pack work, because the pack's entire design philosophy is "images are nparrays, and you're responsible for their channel layout."

The reason it's mandatory: ComfyUI and OpenCV disagree on what a "normal image" is. ComfyUI's IMAGE is a torch tensor - RGB, float32, values 0–1, shape (batch, H, W, C). OpenCV nparrays are numpy - BGR or grayscale, uint8, values 0–255, shape (H, W, C). The pack's Image2Nparray conversion flips RGB→BGR for you, and Nparrays2Image flips back on the way out. But anything that needs grayscale, or a specific channel order, goes through cvtColor_0 first. This is the same space the KB's post-processing.md essay calls "the deterministic pixel operations Photoshop and OpenCV have had for thirty years" - nothing here runs a model, it just moves bytes around.

How it works

The code input is an integer enum, and the README's short list covers 90% of what you'll do:

  • 6 = BGR2GRAY - the grayscale conversion you'll reach for constantly
  • 8 = GRAY2BGR - back to 3-channel color
  • 4 = BGR2RGB - channel order flip
  • 2 = BGR2RGBA, 3 = RGBA2BGR, and so on

OpenCV has hundreds of these codes (BGR2HSV is 40, BGR2LAB is 44), and you can look them up in the official docs. The inputs that matter:

  • src (NPARRAY) - your input image, straight from Image2Nparray.
  • code (INT) - the conversion enum, like 6.
  • dstCn (INT) - desired output channel count; leave it 0 to let OpenCV infer from the code.
  • hint (INT) - a color-conversion hint, 0 (default) is fine for everything you'll do.
  • dst (NPARRAY, optional) - an out-parameter; the README tells you to just avoid these. Leave it empty.
  • nparray (NPARRAY, output) - the converted image, ready for the next OpenCV node or Nparrays2Image.

The _0 / _1 twins, demystified

cvtColor_0 and cvtColor_1 are identical wrappers. The generator made one per overload in cv2's Python type stubs (MatLike vs UMat), and ComfyUI can't tell the difference. Pick either; they call the same cv2.cvtColor under the hood.

Install and gotchas

Install via ComfyUI Manager (search opencv-comfyui) or:

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

Restart ComfyUI. No models. Use opencv-contrib-python (the README's opencv-python-contrib isn't the real package name); conflicting OpenCV installs show up as Cannot import name 'guidedFilter' from 'cv2.ximgproc', with a known fix.

The error you'll actually hit with cvtColor: img.type() == CV_8UC1 assertion failures, because a function wanted grayscale and got color (or vice versa). That's your signal to run cvtColor with code 6 first. And remember the pack only handles batch_size==1 - if you feed it a batch, Image2Nparray will refuse with a clear message, so pull a single frame out first.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
srcNPARRAY
codeINT
dstCnINT
hintINT
dstoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY