RapidOCR JSON Polygon Extend
Give every text polygon room to breathe
- image
- json_data
RapidOCR returns tight polygons around text, and a tight polygon makes a tight inpaint mask - which means the model gets almost nothing to work with around the glyphs, and your "text removal" leaves a ghost. This node fixes that by expanding every polygon outward by a fixed number of pixels while keeping its geometry intact.
The clever bit is in how it expands. A naive approach replaces each polygon with a bigger rectangle, flattening rotated or skewed text into a box. This node maps the original polygon into its expanded enclosing rectangle instead, so a tilted word stays tilted and only grows. That matters when the surrounding pixels matter - which is to say, for inpainting, always.
How it works
Each polygon in the JSON gets expanded independently by expand pixels per side (default 8). Existing box/bbox metadata is updated to match the new envelope, while text, IDs, confidence scores, wrappers, and every other JSON field pass through untouched. Image bounds can come from image_width/image_height, the optional image, or the RapidOCR wrapper's width/height. With clip_to_image on (default), output stays inside the image; turn it off to allow out-of-image coordinates.
image_edge_margin (default 0) is a nice touch for cleanup work: it keeps newly expanded polygon edges at least that many pixels away from the image border - so you don't accidentally create a white band at the edge of the frame. If an original polygon is already closer than the margin, it isn't shrunk; that side just stays put.
Inputs
json_data- the RapidOCR polygon JSON, from a STRING socket.expand- pixels per side, default8. This is your mask-padding knob.image_edge_margin- keep expansion away from image borders (requires known dimensions).clip_to_image- clip output to image bounds (on by default).image_width/image_height, optionalimage,coord_base(0for pixels).
Output: json_data - the same JSON, expanded.
Installing
Part of comfyui-dsocr-bbox. ComfyUI Manager (search "dsocr") or:
cd ComfyUI/custom_nodes
git clone https://github.com/maomaozi/comfyui-dsocr-bbox
restart, then:
pip install -r custom_nodes/comfyui-dsocr-bbox/requirements.txt
Gotchas
Remember the output is JSON, not a mask - pair this with RapidOCR JSON Polygon To Mask (or the GLM/DeepSeek mask nodes) to get an actual mask. If you set image_edge_margin without providing image dimensions anywhere, it can't enforce the margin, so keep image connected or the width/height filled in. And if your expanded regions look blocky, check that you're feeding polygon JSON in - rectangles in, rectangles out.
Inputs (8)
| Name | Type | Default | Description |
|---|---|---|---|
| json_data | STRING | — | |
| expand | INT | 80–10000 | — |
| image_edge_margin | INT | 00–10000 | — |
| image_width | INT | 00–100000 | — |
| image_height | INT | 00–100000 | — |
| coord_base | INT | 00–100000 | — |
| clip_to_image | BOOLEAN | true | — |
| imageopt | IMAGE | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| json_data | STRING | — |