Nodes/Knodes/Load Image (Base64)
ComfyUI Node

Load Image (Base64)

Feed an image into ComfyUI from a string, not a file

By kft334·Created 3 years ago·Updated 2 years ago· 4
Load Image (Base64)
    • IMAGE
    • MASK
    image

    Sometimes the image you want to work on never exists as a file on the ComfyUI machine. It's a screenshot from your own app, an upload from a browser, a frame from another service - it arrives as a base64 string over a wire. ComfyUI's stock Load Image only reads from disk, which forces you to write the thing out to input/ first just to get it into a graph. This node skips that: you hand it the base64 and it hands back the exact IMAGE/MASK pair ComfyUI knows how to consume.

    How it works

    The mechanism is short and transparent. Load Image (Base64) from the kft334/Knodes pack base64-decodes the string, opens it with PIL, converts to RGB, and scales to a float32 tensor in 0–1 range - the standard [1, H, W, 3] image tensor. Then it checks for an alpha channel. If the PNG has one, it derives the mask as 1 - alpha, matching ComfyUI's own convention where transparent areas become the masked (erasable) region. No alpha? You get an empty 64×64 zero mask, exactly what core LoadImage returns for an opaque image.

    So the outputs are:

    • IMAGE - a batch-of-one tensor, ready for VAE Encode, img2img, or any image input port.
    • MASK - usable for inpainting pipelines; it's an empty mask unless your source PNG has transparency.

    The one gotcha that actually bites

    The image input is a single-line string, and it must be the bare base64 payload. The common data:image/png;base64, prefix will make base64.b64decode throw an invalid-encoding error, so strip it before it hits the node. Same story for line breaks: the field is single-line only, and a formatter that wraps base64 at 76 characters will break the decode. Most APIs hand you unwrapped base64, but it's the first thing to check when the node red-screens.

    When you'd reach for it

    Any time an image enters the graph without touching disk: your web frontend uploading a reference into an img2img pass, a script injecting a phone screenshot into a workflow, an automation layer feeding a queue. Pair it with the pack's Image(s) To Websocket (Base64) node and you have a full round trip - your app ships an image in as a string and gets the result back as a string.

    Installing Knodes

    Both of those nodes live in the same pack, so one install gets you everything. No requirements.txt, no model files - the pack only uses PIL, numpy and torch, which ComfyUI already ships. Install via ComfyUI Manager (search "Knodes") or manually:

    cd ComfyUI/custom_nodes
    git clone https://github.com/kft334/Knodes
    

    Then restart ComfyUI. That's the whole setup.

    A note on the batch-of-one shape

    The output is a [1, H, W, 3] tensor, meaning a batch of exactly one - it plugs straight into anything expecting a batch, no reshuffling. Just don't expect embedded workflow metadata like a file-loaded PNG would carry; this is runtime data, not an archival artifact. For that you'd want the disk loader.

    CategoryKnodes

    Inputs (1)

    NameTypeDefaultDescription
    imageSTRING

    Outputs (2)

    NameTypeDescription
    IMAGEIMAGE
    MASKMASK