Nodes/ComfyUI_OpenCV/Color Space Convert
ComfyUI Node

Color Space Convert

OpenCV's Color Space Convert, decoded

By PiggyDance·Created about a year ago·Updated about a year ago· 1
Color Space Convert
  • image
  • image
codeBGR2GRAY

Want a grayscale version of your image without rebuilding it from channel math? This is the node. One input, one dropdown, and you've got a proper luminance image - or an HSV or Lab one, if that's your thing. It's a thin wrapper around OpenCV's cvtColor, so the output is byte-for-byte the same thing every OpenCV tutorial has produced for a decade.

It comes from ComfyUI_OpenCV, a tiny three-node pack by a developer going by PiggyDance. Don't go to the repo README for documentation, by the way - it's still the cookiecutter template the project was generated from ("A list of features" and all). The node is the documentation.

How it works

Under the hood it's cv2.cvtColor with a fixed map of eight codes:

  • BGR2GRAY / GRAY2BGR
  • BGR2RGB / RGB2BGR
  • BGR2HSV / HSV2BGR
  • BGR2Lab / Lab2BGR

The node takes your IMAGE tensor (float 0–1, batch-first, height-width-channel, RGB), scales it to 0–255 uint8, runs the conversion, and scales back. One thing to get straight early: ComfyUI images are already RGB, so those BGR names are OpenCV's house style, not a statement about your pixels. BGR2RGB and RGB2BGR both just swap the R and B channels - the node never converts your RGB tensor to BGR first. It works; the naming just lies to you.

The inputs and outputs

You only ever set one thing: the code dropdown. image is the wire in, image is the wire out - same IMAGE type, so it drops into whatever you were already feeding. Nice detail: the BGR2GRAY path re-stacks the single gray channel back to three channels, so the output stays a standard RGB-shaped IMAGE and downstream nodes never notice the difference.

The two conversions you'll actually use:

  • BGR2GRAY - true luminance, which is what people usually mean by "grayscale" (not naive averaging). Feed it to a ControlNet preprocessor, a mask, or a composite.
  • BGR2HSV - hue/saturation/value. Be warned: in OpenCV's uint8 world hue lives in 0–179 and S/V in 0–255, so after this node normalizes to 0–1 the H channel sits in the 0–0.7 range. An HSV result viewed as RGB looks like a corrupted rainbow - that's normal. Use it when you plan hue-based filtering and convert back with HSV2BGR.
  • BGR2Lab - the L channel is a perceptually meaningful lightness, cleaner than luminance if you're doing tonal work.

The other five choices exist mostly so you can round-trip.

Install

One install covers all three nodes in the pack. Via ComfyUI Manager: open Manager → "Custom Nodes Manager" → search ComfyUI_OpenCV → install → restart. Or manually:

cd ComfyUI/custom_nodes
git clone https://github.com/PiggyDance/ComfyUI_OpenCV
# restart ComfyUI

No model downloads, no giant dependencies. One real gotcha: the pack declares zero pip dependencies, yet its code imports cv2 at load time - so if opencv isn't in your environment, the whole pack silently fails and the node never appears in the menu. A fresh ComfyUI install doesn't guarantee OpenCV anymore (it's no longer in core requirements), so if the node is missing, check your console for ModuleNotFoundError: No module named 'cv2' and fix it:

# in the Python env you run ComfyUI with
pip install opencv-python-headless

Where people get burned

  • Round trips are lossy. The conversion snaps to uint8, so BGR2HSV then HSV2BGR twice drifts. Don't chain conversions for fun; convert once, do your work, convert back.
  • It's CPU-only. The implementation loops over the batch and moves each frame to CPU. Fine for a still, sluggish if you feed it a hundred video frames.
  • It's a small pack, and it shows. Nothing here that a couple of core ComfyUI math nodes can't fake for the gray case - the win is one node, zero math, OpenCV-exact results.
CategoryOpenCV/Color

Inputs (2)

NameTypeDefaultDescription
imageIMAGEThe input image to convert color space
codeCOMBOBGR2GRAYColor conversion code

Outputs (1)

NameTypeDescription
imageIMAGE