Nodes/opencv-comfyui/OpenCV getTextSize_0
ComfyUI Node

OpenCV getTextSize_0

Measure your captions before you draw them

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV getTextSize_0
    • literal
    • int
    text
    fontFace
    fontScale
    thickness

    Watermarks, captions, labels on a contact sheet - if you're burning text onto generated images with OpenCV, you eventually need to know how wide the text is going to be before you draw it. That's the whole job of getTextSize_0. It wraps cv2.getTextSize, the measurement function that answers "if I draw this string with this font at this size, what rectangle will it occupy?" You use it to center a caption, size a box around a label, or check whether your text even fits in the image before the putText node runs.

    The inputs

    Four of them, all dead simple:

    • text (STRING): the string you plan to draw. Note it measures exactly what you type - same string, same font, same result.
    • fontFace (INT): the HERSHEY font index, 07. 0 (FONT_HERSHEY_SIMPLEX) is the default and what you'll use. It's the same font constants putText uses, so keep them matched.
    • fontScale (FLOAT): the size multiplier, 1.0 being a reasonable baseline. 2.0 is roughly double.
    • thickness (INT): stroke width in pixels, matching what you'll pass to putText.

    The outputs

    This is where the auto-generated nature shows. OpenCV returns a tuple: the text's bounding Size plus the baseline offset. The pack exposes them as two sockets:

    • literal (STRING): the size as a Python literal, e.g. (143, 32) - width, height in pixels.
    • int (INT): the baseline, the number of pixels from the bottom of the text box to the line the text actually sits on.

    The literal output being a string is classic opencv-comfyui: you read it, or feed it into a text-processing node that parses it. It's not a first-class Size object you can plug into other nodes, so plan around that. The baseline matters for vertical centering - for the baseline value in putText, which anchors text to a reference line, you'll want it.

    Install

    Same as every node in this pack: ComfyUI Manager (search "opencv-comfyui") or

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

    then restart. The pack needs opencv-contrib-python (pip installs it, or Manager does), plus numpy and torch which ComfyUI already ships. No models, no keys.

    Gotchas

    • The invalid syntax (<unknown>, line 0) error can't happen here - no composite inputs. This one's the easy member of the family.
    • If you measure with fontScale 1.0 and draw with 0.8, your centering math is off by a consistent factor. Measure with the exact values you draw with.
    • Startup error Cannot import name 'guidedFilter' from 'cv2.ximgproc'? That's the classic conflicting-OpenCV-packages problem this pack's README flags - one opencv-contrib-python install, no duplicates, and it goes away.

    For a graph node it's a humble utility, but it's the difference between captions that land dead-center and text that runs off the edge. Reaching for it alongside putText in the same pack is the intended move.

    Categoryimage/OpenCV

    Inputs (4)

    NameTypeDefaultDescription
    textSTRING
    fontFaceINT
    fontScaleFLOAT
    thicknessINT

    Outputs (2)

    NameTypeDescription
    literalSTRING
    intINT