How to Fix OpenCV putText CV_8U Error in ComfyUI Custom Nodes

If you’ve been experimenting with FLUX workflows and image compositing custom nodes in ComfyUI, you might have hit a sudden roadblock that stops your entire queue cold. You set up your prompt, hit Queue Prompt, wait for the sampling to complete, and right when the final composite node executes—boom! You get a scary OpenCV traceback.

Specifically, the error log throws something like this:

cv2.error: OpenCV(5.0.0) ... error: (-215:Assertion failed) img.depth() == CV_8U in function 'putText'

I recently bumped into this exact error while running an image edit workflow using the KleinEditComposite custom node (Node 223) on a remote GPU instance. It reminded me of my old 486-era PC days—spending hours troubleshooting an issue, only to realize a single mismatched byte or faulty RAM stick was causing endless Windows reinstalls!

In this guide, I’ll break down why this OpenCV error happens, how to bypass it quickly in your workflow, and how to permanently patch the Python code so your renders complete smoothly every time.

Why Does This OpenCV Error Happen?

To understand the fix, it helps to understand how ComfyUI handles image data under the hood compared to OpenCV.

ComfyUI processes images as PyTorch tensors and NumPy arrays formatted in 32-bit floating-point (float32), with pixel values normalized between 0.0 and 1.0. This high-precision format is great for deep learning models and image math.

OpenCV, on the other hand, is a classic C++ computer vision library. Functions like cv2.putText()—which custom nodes use to draw text labels onto side-by-side debug images—strictly require an 8-bit unsigned integer array (uint8) where pixel values range from 0 to 255.

When a custom node creates a float32 canvas array and passes it straight into cv2.putText(), OpenCV checks the array’s data depth. Finding floats instead of 8-bit integers, C++ throws an assertion failure: img.depth() == CV_8U.

Credit Where Credit Is Due

Before diving into the code, huge credit goes to supermansundies, the creator of the comfyui-klein-edit-composite custom node repository. It’s a fantastic node for automatic delta-E thresholding, optical flow warping, and image blending when editing FLUX outputs.

Like many open-source tools created by busy developers, small edge-case bugs in helper utilities—like side-by-side debug gallery generators—can pop up when running newer OpenCV versions.

Method 1: The Quick Workflow Fix (No Code Editing)

If you just need your image composite saved right now and don’t care about seeing the internal debug preview images, you can disable debug mode directly in your workflow.

When debug mode is disabled, the node skips calling the side-by-side image generation function altogether, avoiding the bug completely.

Here is how to do it:

  1. Open your ComfyUI workspace and locate Node 223 (KleinEditComposite).
  2. Look at the node options or widgets list for enable_debug.
  3. Toggle enable_debug from true to false.
  4. Re-queue your prompt.

If you are running headlessly or automating workflows via JSON files, open your workflow JSON file (like Klein Edit Composite 01.json), find Node 223, and update the last item in widgets_values from true to false:

JSON

"widgets_values": [
  -1,
  "medium",
  false,
  -1,
  0.3,
  0.5,
  false,
  true,
  0,
  0,
  2,
  1,
  true,
  "replace",
  false
]

Note: Always back up your workflow JSON file before editing raw values by hand!

Method 2: The Permanent Python Code Patch

If you want to keep the debug gallery previews working so you can visually compare original vs. edited pass alignments, you need to fix the Python function in the node source code.

This method requires editing a single Python file inside your custom nodes directory.

Step 1: Open the Custom Node File

Navigate to your ComfyUI installation directory on your server or local machine:

/path/to/ComfyUI/custom_nodes/comfyui-klein-edit-composite/klein_edit_composite.py

Open klein_edit_composite.py in your favorite code editor (VS Code, EditPlus, or nano).

Step 2: Locate _create_side_by_side

Look for the helper function near line 130 that looks like this:

Python

def _create_side_by_side(img1, img2, labels=("Original", "Generated")):
    """Create side-by-side comparison with labels."""
    h, w = img1.shape[:2]
    canvas = np.zeros((h + 40, w * 2, 3), dtype=np.float32)
    canvas[40:, :w] = img1
    canvas[40:, w:] = img2

    cv2.putText(canvas, labels[0], (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (1, 1, 1), 2)
    cv2.putText(canvas, labels[1], (w + 10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (1, 1, 1), 2)
    return canvas

Notice line 4: dtype=np.float32. That is the culprit!

Step 3: Replace with the Fixed Function

Replace the entire _create_side_by_side function with this updated version:

Python

def _create_side_by_side(img1, img2, labels=("Original", "Generated")):
    """Create side-by-side comparison with labels."""
    # Convert incoming float32 [0.0, 1.0] arrays to uint8 [0, 255]
    def _to_u8(img):
        if img.dtype != np.uint8:
            if img.max() <= 1.0:
                img = img * 255.0
            return np.clip(img, 0, 255).astype(np.uint8)
        return img

    u1 = _to_u8(img1)
    u2 = _to_u8(img2)

    h, w = u1.shape[:2]
    # Create an 8-bit unsigned integer canvas (CV_8U) for OpenCV
    canvas = np.zeros((h + 40, w * 2, 3), dtype=np.uint8)
    canvas[40:, :w] = u1
    canvas[40:, w:] = u2

    # Draw text using 8-bit RGB white color (255, 255, 255)
    cv2.putText(canvas, labels[0], (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
    cv2.putText(canvas, labels[1], (w + 10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)

    # Convert back to float32 [0.0, 1.0] expected by ComfyUI
    return canvas.astype(np.float32) / 255.0

Step 4: Save and Restart ComfyUI

Save the changes to klein_edit_composite.py and restart your ComfyUI backend process. Because Python caches imported modules, a restart is necessary for ComfyUI to load the updated code into memory.

Quick Recap & Next Steps

When OpenCV throws Assertion failed (img.depth() == CV_8U), it’s almost always a data type mismatch between ComfyUI’s native float32 arrays and OpenCV’s uint8 image requirements.

To fix it:

  • Fast Way: Disable debug image previews on the node or in your workflow JSON.
  • Clean Way: Convert arrays to uint8 before calling cv2.putText(), then convert back to float32 / 255.0.

Now your FLUX compositing node will render both final images and debug galleries without throwing exception errors halfway through your queue.

Happy generating, and as always—keep your code clean and your servers cool!

No Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.