Nodes/opencv-comfyui/OpenCV rectangle_2
ComfyUI Node

OpenCV rectangle_2

Draw a box from x, y, width, height — rectangle_2 vs rectangle_1

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV rectangle_2
  • img
  • nparray
rec
color
thickness
lineType
shift

rectangle_2 draws a rectangle on an OpenCV image - same job as its sibling rectangle_1, but with a friendlier input shape. Instead of two corner points, it takes a single rec rectangle in (x, y, width, height) form. If you already have a box as four numbers - from a YOLO detector, a face landmark, a crop you computed - this is the node that matches your data without you converting anything.

It's part of opencv-comfyui (geroldmeisinger/opencv-comfyui), the auto-generated pack that wraps OpenCV's standalone functions. The rectangle family is the clearest example of how the numbering works: cv2.rectangle has two overloads in OpenCV's type stubs - one taking two corner points (pt1, pt2, which is rectangle_0/rectangle_1) and one taking a whole Rect (rectangle_2/rectangle_3). You get both, numbered, because the pack parses the stubs literally and emits one node per overload.

What you actually set

  • img - an NPARRAY from Image2Nparray. Remember, this pack lives in OpenCV's numpy world (BGR, 0–255), not ComfyUI's IMAGE tensors.
  • rec - a Rect literal as a string: (10, 20, 100, 80) means x=10, y=20, width=100, height=80. It's width/height, not a second corner. People coming from rectangle_1's pt2 habit type (10, 20, 110, 100) and get a bigger box than they meant.
  • color - a Scalar literal in BGR: (0, 255, 0) is green. Yes, green. Get used to it.
  • thickness - pixels; a negative value fills the box.
  • lineType - 8, 4, or 16 (LINE_AA). Anti-aliasing is 16.
  • shift - subpixel shift, leave it at 0.

Output is one nparray with the box drawn, which goes into Nparrays2Image and back into normal ComfyUI land.

Installing it

ComfyUI Manager (search "opencv-comfyui") or:

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

restart, and make sure OpenCV is present:

pip install opencv-contrib-python

That's the whole dependency story - numpy and torch ship with ComfyUI already. No models, no GPU.

Where people get burned

The invalid syntax (<unknown>, line 0) error means your rec literal didn't parse. (10, 20, 100, 80) parses; 10, 20, 100, 80 doesn't - the parens are required. Same for color.

A subtle one specific to this node: because rec is a literal parsed from a string, you can't wire a computed bounding box into it directly. If a detector node gives you x/y/w/h as separate wires, you have to assemble the literal string yourself (a text-combine node can do it). That's the cost of the pack's "type composite params as Python literals" design, and it's why rectangle_2 is genuinely more convenient when your box is a fixed value you can type once.

Finally, remember the BGR thing. If your green box renders blue, you're not confused - that's just the order OpenCV uses, and cvtColor (code=4, BGR2RGB) is the explicit fix downstream.

Categoryimage/OpenCV

Inputs (6)

NameTypeDefaultDescription
imgNPARRAY
recSTRING
colorSTRING
thicknessINT
lineTypeINT
shiftINT

Outputs (1)

NameTypeDescription
nparrayNPARRAY