ComfyUI Node

image2mask

Batch background-removal masks for every frame of your video

By dafeng012·Created 2 years ago·Updated 2 years ago· 17
image2mask
    • output_dir
    input_dir
    output_dir
    tb_use_fast_modetrue
    tb_use_jittrue
    st1_mask_threshold0.0
    device

    image2mask is the mask factory for the EBSynth pipeline: point it at a folder of frames and it runs automatic background removal on every one, writing a matching mask for each. In the comfyui-imgmake workflow this is the step that decides what EBSynth is allowed to restyle - and if you've ever done style transfer on video, you know the mask quality is the result quality. Garbage masks mean EBSynth propagates garbage everywhere.

    The tool underneath is transparent_background, the library built around InSPyReNet - an image-pyramid segmentation model that's been a community favorite for years (and still gets "absolute magic" reviews in background-removal threads). It's not the newest toy in the box; the pack just ships it as the default because it's reliable on complex edges like hair.

    How it works

    For each PNG in your input folder, the node runs the InSPyReNet remover, gets back a soft matte, then hard-thresholds it: anything below st1_mask_threshold gets zeroed out. The result is written as a grayscale mask PNG with the same filename as its source frame. It's a pure batch job - nothing goes to the GPU sampler, no diffusion involved.

    There's a subtlety in the paths. The node takes an input_dir and output_dir, but then appends subfolders to both: it reads from <input_dir>/video_frame/ and writes to <output_dir>/video_mask/. So you pass it your project directory, not the frames folder directly. Feed it project_dir and it does the right thing for the EBSynth layout.

    The inputs that matter

    • input_dir - your project dir; the node appends video_frame.
    • output_dir - your project dir; the node appends video_mask.
    • st1_mask_threshold - 0 to 0.5. The cutoff for what counts as "foreground". Too low and background noise creeps in; too high and you'll eat thin details like hair. 0 (the default) keeps the raw soft mask.
    • tb_use_fast_mode / tb_use_jit - speed switches for the remover. Keep them on unless you hit memory or precision problems; turning them off is slower but sometimes steadier.
    • device - cuda:0 or cpu. On CPU this will be painfully slow for a whole video's worth of frames.

    It returns one STRING, output_dir - the path it actually wrote masks to - which you can thread into the next node that needs the mask location.

    Installing it

    Part of the comfyui-imgmake pack - Manager (search comfyui-imgmake) or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/dafeng012/comfyui-imgmake
    

    Restart, and install the heavy dependency the README calls out separately:

    pip install transparent-background
    

    (It's in requirements.txt too, but the README's separate step is a hint that people miss it.) First run downloads model weights into ~/.transparent-background; if that fails, the README tells you to use a proxy or drop the model files in manually - there's a Baidu Pan pack linked for exactly this.

    Troubleshooting

    • No masks written - the node only globs *.png under video_frame, so run video2image (frame extraction) first. JPEG frame dumps won't match the glob.
    • Hair and edges look chewed - lower st1_mask_threshold toward 0 so the matte's soft edges survive thresholding, and consider turning tb_use_fast_mode off for a higher-quality pass.
    • Crash on import - transparent_background needs torch; if your ComfyUI env is torch-adjacent but missing pieces, the separate pip install is usually the fix.

    One honest caveat: this is a blunt instrument compared to a hand-drawn mask. For a subject that's easy to segment, great. For complex scenes, expect to clean up the important keyframes yourself - and honestly, that's where you should spend the effort, since EBSynth only needs great masks on the frames you actually paint.

    CategoryIMAGEmake

    Inputs (6)

    NameTypeDefaultDescription
    input_dirSTRING
    output_dirSTRING
    tb_use_fast_modeBOOLEANtrue
    tb_use_jitBOOLEANtrue
    st1_mask_thresholdFLOAT0.0
    deviceCOMBO2 options: cuda:0, cpu

    Outputs (1)

    NameTypeDescription
    output_dirSTRING