OpenCV rectangle_1
Draw rectangles on images in ComfyUI — the OpenCV way
- img
- nparray
rectangle_1 is the boring-but-useful one: it draws a rectangle on an image. Think annotation, not generation. You reach for it when you want to mark the region a detection node found, visualize a crop before it hits an inpaint/detailer pass, or stamp a colored box onto a finished frame for review. ComfyUI has a few ways to overlay boxes, but this is the OpenCV-precise one, with anti-aliased edges and subpixel positioning if you want them.
It's part of opencv-comfyui (geroldmeisinger/opencv-comfyui), a pack that auto-generates a ComfyUI node for almost every top-level OpenCV function. That matters for one reason: nothing in this pack touches ComfyUI's IMAGE type directly. Every node works in OpenCV's world - numpy arrays, BGR order, 0–255 values. So the rectangle you draw lives in NPARRAY land, and you need Image2Nparray to get in and Nparrays2Image to get back out.
What you actually set
img- anNPARRAY, straight fromImage2Nparray.pt1,pt2- the two corner points, as literal strings. This is the pack's quirk: composite types like Point are typed as Python literals and parsed withast.literal_eval. So(50, 50)and(400, 300)work;50, 50without parens does not.color- a Scalar literal, and here's the trap: it's BGR, not RGB.(0, 255, 0)is bright green, not blue. If your box comes out with swapped channels, this is why.thickness- anINT, in pixels. Negative fills the box solid, which is handy for a color chip or a mask overlay.lineType-8(default, 8-connected),4, or16(LINE_AA, anti-aliased). Use 16 for anything you'll actually look at.shift- subpixel shift, a specialist thing. Leave it at 0.
The output is a single nparray, the image with the box baked in, which you convert back with Nparrays2Image and feed onward.
Installing it
Either install through ComfyUI Manager (search "opencv-comfyui"), or:
cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
then restart ComfyUI. The pack's requirements.txt is just opencv-contrib-python, numpy, torch - you already have the last two, so the only thing you usually add is:
pip install opencv-contrib-python
No model files, no downloads, nothing GPU-heavy - this is a pure CPU numpy call.
Where people get burned
The invalid syntax (<unknown>, line 0) error is the pack's signature complaint: it means a literal string didn't parse. (400, 300) parses; 400, 300 doesn't. Same story for color.
Second, the batch trap: Image2Nparray rejects anything with batch_size > 1 with an explicit error. If your image comes out of a batch, run it through ImageFromBatch with length=1 first.
And if the colors look wrong, stop and check you're thinking BGR. It's not a bug - it's OpenCV being OpenCV. cvtColor with code=4 (BGR2RGB) is the explicit way to fix a mismatch downstream.
One honest caveat: rectangle_0 and rectangle_1 are the same cv2.rectangle overload, emitted twice because the type stubs list it twice. Either works; they're interchangeable.
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 | — |