Nodes/opencv-comfyui/OpenCV getFontScaleFromHeight_0
ComfyUI Node

OpenCV getFontScaleFromHeight_0

The Node That Sizes Text So Captions Don't Overflow Your Image

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV getFontScaleFromHeight_0
    • float
    fontFace
    pixelHeight
    thickness

    OpenCV getFontScaleFromHeight_0 solves one small, irritating problem: OpenCV's putText doesn't take a font size in pixels - it takes a unitless scale. So how do you get a caption that's exactly, say, 40 pixels tall without trial and error? You ask cv2.getFontScaleFromHeight(fontFace, pixelHeight, thickness), which returns the scale value that makes capital letters land at roughly the pixel height you asked for. That's this node. It's the sizing helper for anyone burning text onto images with the pack's putText node.

    Why you'd reach for it

    Text overlays are a recurring post-processing chore: watermarking, burning in prompt metadata, labeling crops for contact sheets or batch-review dumps. The pattern is always the same - you know how tall you want the text to be in the image, not in some abstract scale unit. Feed your desired height in, get a float scale out, and wire that straight into putText's fontScale. Without it you're back to guessing scale values and re-rendering until it looks right.

    The inputs

    • fontFace (INT) - the OpenCV Hershey font constant, matching what you'll pass to putText. Common ones: 0 = FONT_HERSHEY_SIMPLEX, 1 = PLAIN, 3 = COMPLEX. The italic variants are the base value plus 16. Mismatch this with the putText fontFace and your height math is wrong.
    • pixelHeight (INT) - the target height of the text in pixels. This is the number you actually care about.
    • thickness (INT) - the stroke thickness you plan to draw with. It matters because thicker strokes change the effective height, so this should match the thickness you use in putText.

    Output is a single float - the fontScale to pass to putText. There's no image in or out here; this node is pure arithmetic between your text-drawing nodes.

    What to watch for

    It's approximate. OpenCV sizes the scale from the capital-letter height, and the result is a good estimate, not a promise - italic and script fonts drift more than the plain ones, and thin vs. thick strokes shift the visual baseline. Plan to nudge pixelHeight up or down a few pixels after eyeballing the first render. Also note the height is about the glyphs, not the descenders, so a pixelHeight of 40 gives you ~40px caps with a bit of extra room below.

    Install

    Same as the rest of the pack: ComfyUI Manager → search opencv-comfyui (display "OpenCV"), or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/geroldmeisinger/opencv-comfyui
    pip install opencv-contrib-python
    

    Restart, done. No model files.

    Usual pack reminders apply: this node has no image inputs, but the images your putText node works on must come in as NPARRAY via Image2Nparray (batch size 1), and any composite inputs elsewhere in the workflow need bracket-literal syntax. If you get the fontFace wrong, you won't get an error - you'll just get silently mis-sized text, which is honestly worse.

    Categoryimage/OpenCV

    Inputs (3)

    NameTypeDefaultDescription
    fontFaceINT
    pixelHeightINT
    thicknessINT

    Outputs (1)

    NameTypeDescription
    floatFLOAT