Nodes/opencv-comfyui/OpenCV rectangle_0
ComfyUI Node

OpenCV rectangle_0

Draw boxes on an image — the BGR gotcha included

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV rectangle_0
  • img
  • nparray
pt1
pt2
color
thickness
lineType
shift

rectangle is OpenCV's "draw a box" function, exposed as a node: two corner points, a color, a thickness, and it stamps a rectangle onto your image. In a ComfyUI context that's the annotation move - draw bounding boxes around detected objects (faces, regions) so you can eyeball what a detection stage found, or mark the region you're about to inpaint. It's a genuinely useful little tool for debugging and for building "annotated preview" workflows, and it's one of the few drawing primitives in this pack that sees real use.

The mechanism is plain: cv2.rectangle(img, pt1, pt2, color, thickness, ...) draws a rectangle from corner pt1 to opposite corner pt2, overwriting pixels in place. The output is your input image with the box baked in - that's the whole operation, no compositing, no layers.

Inputs and the traps

  • img - your NPARRAY (BGR after Image2Nparray).
  • pt1, pt2 - corners as string literals, parsed with ast.literal_eval. Write them as tuples: (10, 10) and (200, 300). Wrong syntax = the README's signature invalid syntax (<unknown>, line 0) error.
  • color - a string literal too, and this is the trap: the array is BGR, not RGB. To draw a red box you write (0, 0, 255). Green is (0, 255, 0), blue is (255, 0, 0). People draw a "red" box with (255, 0, 0), see blue, and lose twenty minutes. Don't be that person.
  • thickness - int. 13 for outlines; a negative value (e.g. -1) fills the rectangle solid, which is how you'd make an opaque patch.
  • lineType - int: 4 (LINE_4), 8 (LINE_8, the default-style 8-connected line), 16 (LINE_AA, anti-aliased - use this for smooth edges on previews).
  • shift - int, subpixel shift (default 0). Leave it alone.

Output is a single nparray: your image with the rectangle drawn on, BGR, through Nparrays2Image to view.

Practical notes

Points and color share the same literal-parsing path, so if one tuple parses, they all do - the syntax error is a fast fail that at least tells you exactly which field to fix. Because this mutates the array in place, don't wire your source nparray anywhere else and expect it pristine; if you need the original too, branch before this node.

For the "annotate detections" workflow, remember the pack's batch_size==1 limit and that you need real coordinates - there's no built-in detection or bbox source here. This node draws; it doesn't find.

Install

No models, no downloads - the pack is wrappers over OpenCV:

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

Or search "OpenCV" in ComfyUI Manager, restart, browse image/OpenCV. If load throws Cannot import name 'guidedFilter' from 'cv2.ximgproc', that's conflicting OpenCV packages - known issue with a documented fix in the README. And batch_size==1 only, as with the whole pack.

Categoryimage/OpenCV

Inputs (7)

NameTypeDefaultDescription
imgNPARRAY
pt1STRING
pt2STRING
colorSTRING
thicknessINT
lineTypeINT
shiftINT

Outputs (1)

NameTypeDescription
nparrayNPARRAY