Nodes/comfyui-dsocr-bbox/RapidOCR JSON Polygon Extend
ComfyUI Node

RapidOCR JSON Polygon Extend

Give every text polygon room to breathe

By maomaozi·Created 2 months ago·Updated 2 months ago· 0
RapidOCR JSON Polygon Extend
  • image
  • json_data
json_data
expand8
image_edge_margin0
image_width0
image_height0
coord_base0
clip_to_imagetrue

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, default 8. 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, optional image, coord_base (0 for 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.

CategoryRapidOCR

Inputs (8)

NameTypeDefaultDescription
json_dataSTRING
expandINT80–10000
image_edge_marginINT00–10000
image_widthINT00–100000
image_heightINT00–100000
coord_baseINT00–100000
clip_to_imageBOOLEANtrue
imageoptIMAGE

Outputs (1)

NameTypeDescription
json_dataSTRING