Nodes/ComfyUI CV/cv2.getTextSize
ComfyUI Node

cv2.getTextSize

Measure the label before you draw it

By bmad4ever·Created 4 months ago·Updated 15 days ago· 1
cv2.getTextSize
    • size
    • baseline
    ◄text►
    ◄fontFaceFONT_HERSHEY_SIMPLEX►
    ◄fontScale0.0000►
    ◄thickness0►

    If you've ever drawn text on an image and then had to guess where to put the background box, this node is the answer: it tells you how big the text will be before you draw it. Four inputs, two outputs, no pixels touched. It's the measuring half of putText, and the pack ships both halves - CV Draw Text for the drawing, this wrapper when you need the numbers.

    Inputs

    • text - the string. Straightforward, and note that measuring is cheap, so measure the final string rather than an estimate of it.
    • fontFace - a dropdown of OpenCV's Hershey stroke fonts, defaulting to FONT_HERSHEY_SIMPLEX. The dropdown carries the whole FONT_HERSHEY_* family - PLAIN, DUPLEX, COMPLEX, TRIPLEX, COMPLEX_SMALL, SCRIPT_SIMPLEX, SCRIPT_COMPLEX and the _ITALIC variants. These are vector stroke fonts, not the system font, so the measurement matches what putText will actually render.
    • fontScale - the multiplier on the font's base size. The field defaults to 0, and a zero-size font is not a useful measurement - set it to something like 1.0 and scale from there.
    • thickness - the line thickness in pixels. Measure with the same thickness you're going to draw with. Bold text is wider than thin text, and a box sized from a thin measurement will clip a bold label.

    Outputs

    size is a composite CV_TUPLE - the width and height of the box that contains the text - and it's designed to travel straight into any size-typed cv2 input, or into CV Split Tuple to get w and h as separate numbers.

    baseline is an INT: how many pixels below the text's origin OpenCV puts the baseline for descenders. This is the output that makes the node worth having, because size alone won't centre text. putText positions text by its bottom-left corner, so the vertical extent you actually need to reserve is size.h + baseline, and the origin for a vertically centred label is that much lower than you'd guess. Anyone who has hand-tuned a y offset until it "looked right" has been reimplementing this output badly.

    The canonical recipe, which is exactly how putText is documented:

    1. Measure (w, h) and baseline for your string at your font and scale.
    2. Draw a filled rectangle from your chosen top-left to (x + w, y + h + baseline) - one or two pixels of padding is taste, not arithmetic.
    3. Draw the text at (x, y + h).

    That ordering puts the box exactly around the text at any scale, which is what you want when the label is "0.87" and it's stamping a detection across a batch of frames.

    Where it fits in a graph

    size being a composite is a deliberate ergonomic choice in this pack: one value, so it can't arrive half-connected, and CV Split Tuple is the escape hatch when you need the components. baseline is a plain INT, which means it can go into a converted integer widget, into a CV Tuple component socket, or - for the purely exploratory version of this - into Inspect CV Data, whose summary output you preview as text with core's Preview as Text node. That's the general pattern for any non-image output here, and it's the reason these little introspection and measurement nodes are usable at all: a number with no picture attached still has somewhere to be read.

    For a one-off label, CV Draw Text and the stock text-overlay options in ComfyUI are less work than wiring three nodes. You want this one when text placement is conditional - when the box has to fit the string, the string changes per frame, or the layout has to be computed rather than typed.

    Install

    pip install "opencv-contrib-python-headless~=5.0.0.93"
    

    Then ComfyUI Manager → search ComfyUI CV, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/bmad4ever/comfyui_cv
    

    and restart. Python ≥ 3.12 and a recent ComfyUI (V3 node API) are required; the pack is curated against OpenCV 5.0.0.93. Nothing to download, no models - this is arithmetic on a font table.

    Gotchas

    • Font scale 0 is the default and it's meaningless. Same for thickness at 0. Both want real numbers, and the difference between a measurement and a plausible-looking one is exactly these two fields.
    • Hershey fonts only. The measurement is exact for the fonts this node can draw with, and useless for anything else - if you're compositing with a system font or a node from another pack, its metrics are a different question entirely.
    • Non-contrib wheel trap, which applies to the whole pack: all four OpenCV PyPI distributions share one site-packages/cv2, so installing a non-contrib wheel over a contrib one strips the contrib submodules and a slice of this pack's nodes vanish. tools/repair_opencv_contrib.py --check → --apply.
    • It measures, it doesn't render. No image goes in, none comes out. If your graph has no text drawing node yet, this measurement has nowhere to go - pair it with a putText wrapper or an equivalent drawing node first.
    Categoryimage/CV/low-level/cv2 G

    Inputs (4)

    NameTypeDefaultDescription
    textSTRINGInput text string.
    fontFaceCOMBOFONT_HERSHEY_SIMPLEXFont to use, see #HersheyFonts.
    fontScaleFLOAT0.0000-1e+38–1e+38Font scale factor that is multiplied by the font-specific base size.
    thicknessINT0-2147483648–2147483647Thickness of lines used to render the text. See #putText for details.

    Outputs (2)

    NameTypeDescription
    sizeCV_TUPLEThe size of a box that contains the specified text. Size (width, height) as ONE composite value - wire it straight into a cv2 size input (dsize, patchSize, winSize) or into 'CV Split Tuple'.
    baselineINT—