Nodes/ComfyUI_Swwan/Save Image With Alpha (Swwan) · 旧版兼容
ComfyUI Node

Save Image With Alpha (Swwan) · 旧版兼容

PNG transparency, and the one inversion that bites

By aining2022·Created 10 months ago·Updated about 18 hours ago· 33
Save Image With Alpha (Swwan) · 旧版兼容
  • images
  • mask
    ◄filename_prefixComfyUI►

    Cut a subject out, keep it transparent, save it as a PNG with a real alpha channel. ComfyUI's stock Save Image will not do this for you - it saves RGB and drops the fourth channel on the floor - which is why a small industry of "save with alpha" nodes exists. This is the pack's, and like its neighbours it's the · 旧版兼容 version: the recommended entry point is Save Image (Swwan), which does the same job and quite a lot more.

    Reach for it when an existing workflow already has it (it's a port of KJNodes' SaveImageWithAlpha, so LayerStyle/KJ-era transparency graphs land here), or when you want the absolutely minimal form of the operation: image in, mask in, PNG out, no widgets to think about.

    How it works

    It's PIL end to end, and short enough to describe completely. The image batch is converted to uint8 RGB; the mask is converted to an 8-bit greyscale image and resized to the image's dimensions with LANCZOS (so a mask at a different resolution is not a mismatch error, it's a resample); then putalpha welds the mask on as the alpha channel and the result is written as PNG with compress_level = 4, alongside the prompt/workflow metadata chunks, into ComfyUI's output directory. The counter is derived by scanning the target folder for files matching the prefix, so it picks up where your last run left off instead of overwriting.

    Note the semantics: the mask is the alpha. White in the mask means opaque, black means transparent. It is not "the area to keep"; it's the opacity value, pixel for pixel.

    Inputs and outputs

    Required: images (IMAGE), mask (MASK), filename_prefix (STRING, default ComfyUI). That's the full interface - no quality, no format, no subfolder, no output folder. Outputs: none. The schema returns an empty output list because this node only writes to disk and hands back the UI preview; there's nothing to wire onward, which is correct for a terminal node but occasionally annoys people who wanted the filename.

    Install

    It comes with the pack:

    cd /path/to/ComfyUI/custom_nodes
    git clone https://github.com/aining2022/ComfyUI_Swwan.git
    cd ComfyUI_Swwan
    python -m pip install -r requirements.txt
    

    Python 3.11+, ComfyUI's own torch/numpy/Pillow, plus the pack's opencv-python, scipy and scikit-image. No models to download. Restart and refresh after installing; search Swwan in the node browser.

    Where people get burned

    The mask you loaded is inverted. This is the big one, and it's not a bug in this node. ComfyUI's Load Image gives you MASK as 1 − alpha, because that convention suits inpainting (white = paint here). This node wants actual alpha. Load a transparent PNG, wire its MASK straight in, and you'll get a file where the subject vanished and the background is perfectly preserved. You need the real alpha - the pack ships an RGBA path for exactly this, and its README states the distinction plainly: Load Image's mask is 1 − alpha, the RGBA nodes output the actual alpha. Invert before saving if that's all you have.

    PNG only, and transparency only where the format allows it. Everything else flattens: JPEG and BMP have no alpha, so a downstream conversion to JPEG will composite your subject onto black or whatever the encoder decides. Keep the PNG as the master - it's also the only format that carries the workflow back.

    Mask/image size mismatch is silent. The LANCZOS resize means a wrong-size mask produces a plausible-looking but slightly wrong soft edge rather than an error. If cutouts look mushy, check the mask is at source resolution.

    No outputs means no chaining. If your pipeline wants the saved path to feed something else, use Save Image (Swwan) instead - it returns absolute paths for every image it writes.

    CategorySwwan/Legacy

    Inputs (3)

    NameTypeDefaultDescription
    imagesIMAGE—
    maskMASK—
    filename_prefixSTRINGComfyUI—

    Outputs (0)

    No outputs