Nodes/ComfyUI_Swwan/Load And Resize Image (Swwan)
ComfyUI Node

Load And Resize Image (Swwan)

The loader that does your resizing and pulls a mask out of any channel

By aining2022·Created 10 months ago·Updated about 18 hours ago· 33
Load And Resize Image (Swwan)
    • image
    • mask
    • width
    • height
    • image_path
    ◄image▾►
    ◄resizefalse►
    ◄width512►
    ◄height512►
    ◄repeat1►
    ◄keep_proportionfalse►
    ◄divisible_by2►
    ◄mask_channel▾►
    ◄background_color►

    What it's for

    ComfyUI's stock Load Image gives you an image and a mask derived from alpha - that's it, and it loads at native resolution with no control over size. This node (KJNodes' LoadAndResizeImage) is the loader for people who are tired of bolting a resize node onto every single one.

    It loads from your ComfyUI input folder with the usual upload widget, resizes to whatever you ask for, can repeat the image into a batch, and - the genuinely useful trick - can derive the mask from the red, green or blue channel instead of alpha. Running a depth pass over a photo? Point it at a greyscale depth image and grab the channel. Got a luminance mask baked into a JPEG? Same idea.

    How it works

    repeat is applied at the end: after the image loads and resizes, it's repeated along the batch dimension. That's how you get the load-and-batch in a single node - a 1-image load with repeat=4 arrives downstream as a 4-frame batch. Note it's implemented on the single-image result, so if you point it at a multi-frame file (a GIF or animated WebP) the repeat is skipped and you get the animation's frames instead.

    The resize happens in PIL with BILINEAR, which is fast and slightly soft; for a 4x downscale you'll notice. keep_proportion switches the pair from "these exact numbers" to "fit inside this box", and divisible_by floors both dimensions to a multiple afterwards - set it to 8/16/64 for anything latent-based and leave 0 if you genuinely want the exact numbers.

    mask_channel picks where the mask comes from: alpha, red, green, blue. For alpha the output is 1 - alpha, matching ComfyUI's convention, so opaque areas are mask 0. For R/G/B the channel value is used directly (/255). If the chosen channel isn't present in the file, you get a 64×64 zeros placeholder rather than an error - a silent thing to check when a mask input "doesn't seem to be connected".

    background_color fills the alpha channel with a colour, per the author's tooltip, and it also flattens the image by compositing it onto that colour. Accepts a hex string, a named PIL colour, or an r,g,b tuple. When it's set, the mask switches to the alpha-derived one regardless of mask_channel, which is a subtle coupling: setting a background colour changes your mask source too.

    Outputs: image, mask, width, height and image_path. The integers are the values after resizing and rounding, so they're safe to wire into an empty latent.

    Install

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

    Restart ComfyUI, hard-refresh the tab, search Swwan. Only Pillow and torch are involved, so this works on a CPU-only box. The pack never fetches models and this node doesn't need any.

    Windows portable users can install the requirements from the portable root:

    .\python_embeded\python.exe -m pip install -r .\ComfyUI\custom_nodes\ComfyUI_Swwan\requirements.txt
    

    Common issues

    The file list is empty or stale. The node enumerates ComfyUI/input at the moment the node is defined, i.e. at ComfyUI startup. Drop a file in while the server is running and you may need to refresh the page - or restart - before it shows up in the dropdown.

    Mask is all black / 64×64. The selected mask_channel doesn't exist in that file. Switch to a channel it actually has, or use alpha on an RGBA PNG.

    Downscaled images look soft. BILINEAR in PIL with no antialiasing on aggressive reductions. If quality matters, load at native size and resize with a Lanczos-capable node (this pack has several) instead of relying on the loader.

    A multi-frame file ignored my repeat. Expected: repeat only applies when the load produced a single image.

    CategorySwwan/Advanced/IO

    Inputs (9)

    NameTypeDefaultDescription
    imageCOMBO3 options: brekkie.jpg, example.png, plantpot.png
    resizeBOOLEANfalse—
    widthINT5120–16384—
    heightINT5120–16384—
    repeatINT11–4096—
    keep_proportionBOOLEANfalse—
    divisible_byINT20–512—
    mask_channelCOMBOChannel to use for the mask output
    background_colorSTRINGFills the alpha channel with the specified color.

    Outputs (5)

    NameTypeDescription
    imageIMAGE—
    maskMASK—
    widthINT—
    heightINT—
    image_pathSTRING—