OpenCV rectangle_0
Draw boxes on an image — the BGR gotcha included
- img
- nparray
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- yourNPARRAY(BGR afterImage2Nparray).pt1,pt2- corners as string literals, parsed withast.literal_eval. Write them as tuples:(10, 10)and(200, 300). Wrong syntax = the README's signatureinvalid 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.1–3for 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 (default0). 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.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| img | NPARRAY | — | |
| pt1 | STRING | — | |
| pt2 | STRING | — | |
| color | STRING | — | |
| thickness | INT | — | |
| lineType | INT | — | |
| shift | INT | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| nparray | NPARRAY | — |