Nodes/Duanyll Nodepack/Cover Words with Rectangles
ComfyUI Node

Cover Words with Rectangles

Masking out text for cleaner inpainting

By Duanyll·Created about a year ago·Updated 4 months ago· 2
Cover Words with Rectangles
  • mask
  • MASK
dilation_width_ratio0.020
min_area100

Cover Words with Rectangles takes a text-region mask - the white blobs that mark where words are - and replaces each blob with a clean, solid, rotated rectangle that fully covers it. The output mask is then perfect for inpainting: you tell the sampler "everything under these rectangles is off-limits, regenerate it," and the words disappear. It's the pre-processing step in a text-removal pipeline, and it exists because the pack's author clearly wanted to scrub text from images with the least fuss.

Why rectangles instead of just using the mask as-is? Two reasons. First, the raw mask from a text detector is often a noisy mess of half-connected letter strokes; inpainting against it leaves ghost fragments. Second, rotating rectangles hug the actual text layout - when words are skewed or tilted in the image, an axis-aligned box covers twice the area it needs to. The node computes each rectangle as the minimum-area rotated box around a detected word blob, which covers exactly the text and little else.

How it works

The pipeline is classic computer vision: a morphological close operation merges the individual letter strokes of each word into one connected blob (this is the dilation_width_ratio knob - the close kernel's width as a fraction of image width), then it finds the outer contours, throws away tiny ones below min_area, and for each survivor draws the minimum-area rotated rectangle, filled white, onto a fresh canvas.

Three inputs, all with sane defaults:

  • mask - the input text mask (MASK tensor).
  • dilation_width_ratio - close-kernel width relative to image width, default 0.02. Too small and letters stay disconnected; too big and neighboring words merge into one blob. 0.02 is a good starting point.
  • min_area - contours smaller than this (default 100 pixels) are dropped as noise. Raise it if you're getting specks.

One output: MASK, the canvas of solid rectangles.

Install

The pack standard:

cd ComfyUI/custom_nodes
git clone https://github.com/Duanyll/duanyll_nodepack
cd duanyll_nodepack && pip install -r requirements.txt

or ComfyUI Manager → "Duanyll Nodepack" → install → restart. This node genuinely needs opencv-python-headless (it's in the pack's requirements) - the morphology and contour ops are pure OpenCV.

Where it works and where it doesn't

The intended flow: detect text regions (a mask from a text-segmentation model, or anything that produces word blobs), cover them, feed the result as the mask to any inpainting sampler. After inpainting, the text region gets regenerated from context - watermark-style removal, translating UI mockups, cleaning stock photos. It pairs naturally with the pack's bbox and LLM tooling for a fully automated scrub pipeline.

The honest limits: it operates on a mask, not the image, so if you don't have a text mask it can't help you find the text in the first place. And rotated rectangles are still rectangles - for curved or vertically-stacked text you'll cover more than you'd like. But for the common case of horizontal or lightly-tilted text, this is a surprisingly effective and completely free way to build a cleaner inpainting mask than hand-tracing ever was.

Categoryduanyll/morphology

Inputs (3)

NameTypeDefaultDescription
maskMASK
dilation_width_ratioFLOAT0.0200–0.2
min_areaINT1000–10000

Outputs (1)

NameTypeDescription
MASKMASK