ComfyUI Node

SVG Path Cleanup

Make your vector file small, smooth, and actually usable

By TJ16th·Created 10 months ago·Updated 10 months ago· 2
SVG Path Cleanup
    • cleaned_svg
    • statistics
    svg_string
    remove_short_pathstrue
    min_path_length5.0
    merge_close_pathstrue
    merge_distance2.0
    simplify_pathstrue
    simplify_tolerance0.5
    remove_near_duplicate_pathstrue
    near_duplicate_distance1.5
    round_coordinatestrue
    decimal_places2
    remove_duplicate_pathstrue

    Raw vectorized line art is a hoarder's closet. Every dust speck becomes a path, every doubled edge becomes two nearly-identical paths, and every contour is carrying far more points than it needs. SVG Path Cleanup is the Marie Kondo pass for your SVG: it runs a battery of fixes on the path data itself and hands you back a smaller, cleaner, more machine-friendly file. The README's performance tip says it plainly - for large SVGs, use this to reduce path count.

    What it cleans, one toggle at a time

    All the toggles default on, which is the right call:

    • remove_short_paths / min_path_length (default 5) - deletes paths shorter than this. Your speck-killer. If you set it to 0, nothing gets removed by length.
    • merge_close_paths / merge_distance (default 2) - joins paths whose endpoints sit within this distance, so the same line broken into fragments by the contour finder becomes one continuous path. This is a big one for laser-cutters and plotters, where a broken line means a lift of the head mid-stroke.
    • simplify_paths / simplify_tolerance (default 0.5) - Douglas-Peucker simplification on each path. This is what actually shrinks your file: it drops points that aren't pulling their weight. Tolerance 0.5 is a good starting balance; push it up for aggressive reduction.
    • remove_near_duplicate_paths / near_duplicate_distance (default 1.5) - the interesting one. It treats two paths as duplicates if their bidirectional average distance is below the threshold, then drops one. That's the fix for the double-line problem at the SVG level (Mask Line Cleanup handles it at the pixel level, before tracing).
    • round_coordinates / decimal_places (default 2) - rounds path coordinates so your SVG is human-readable and smaller. Feed a 15-decimal-point path into a plotter and it'll thank you.
    • remove_duplicate_paths (optional, default on) - exact-duplicate removal on top of the near-duplicate pass.

    Outputs: cleaned_svg (feed onward to the savers or style nodes) and statistics (JSON - how many paths removed, merged, simplified; the number you'll actually care about).

    Where it sits in the pipeline

    The pack's own reference flow is: Line Region Detector → Centerline to SVG → SVG Path Cleanup (optional) → SVG File Saver. It's the same cleanup philosophy as Mask Line Cleanup, but one stage later and on geometry instead of pixels. Run it before saving and before auto-reorder - a cleaned, merged, simplified SVG reorders more predictably and is far easier to style by index, because stray duplicate paths aren't inflating your path numbers.

    Install

    The whole pack installs together:

    cd ComfyUI/custom_nodes
    git clone https://github.com/TJ16th/TJ_ComfyUI_Lineart2Vector.git
    # restart ComfyUI
    

    or ComfyUI Manager → search TJ_ComfyUI_Lineart2Vector. No models, no keys. It imports numpy/opencv/scipy from the pack's requirements.txt - all stock ComfyUI territory, so a bare clone usually just works.

    Where people get burned

    The trap is over-cleaning: crank simplify_tolerance too high and fine details - eyelashes, hair strands, thin accent lines - round away into smooth blobs. And merge_close_paths with a generous merge_distance can weld together paths that were supposed to be separate (a closed loop snapped shut, two unrelated lines joined). Check the statistics output and, more importantly, spot-check visually with SVG To Image or SVG Group Layout after cleaning. And note the difference between min_path_length (removes short) and min_component_size on the mask-side node - different stages, both useful, don't confuse their job.

    CategoryTJ_Vector

    Inputs (12)

    NameTypeDefaultDescription
    svg_stringSTRING
    remove_short_pathsBOOLEANtrue
    min_path_lengthFLOAT5.00–100Remove paths shorter than this length
    merge_close_pathsBOOLEANtrue
    merge_distanceFLOAT2.00–20Merge paths if endpoints are within this distance
    simplify_pathsBOOLEANtrue
    simplify_toleranceFLOAT0.50–5Path simplification tolerance
    remove_near_duplicate_pathsBOOLEANtrue
    near_duplicate_distanceFLOAT1.50–10Treat two paths as duplicates if their bidirectional average distance is below this threshold
    round_coordinatesBOOLEANtrue
    decimal_placesINT20–6
    remove_duplicate_pathsoptBOOLEANtrue

    Outputs (2)

    NameTypeDescription
    cleaned_svgSTRING
    statisticsSTRING