Nodes/comfyui-spawner-nodes/图像/蒙版转Base64
ComfyUI Node

图像/蒙版转Base64

Export an image (and optionally its mask) as a base64 data URI

By spawner1145·Created about a year ago·Updated 11 months ago· 2
图像/蒙版转Base64
  • image
  • mask
  • base64_str
add_data_prefixtrue

Every so often your workflow's output needs to leave ComfyUI as text - an HTTP call, a script, an LLM node, a save-to-database step. ImageMaskToBase64 does the conversion: it takes an IMAGE (and optionally a MASK) and hands you a base64_str string you can paste anywhere a data URI is accepted.

The details that matter:

  • image (required) - the image. One sharp edge: it encodes image[0], the first frame of the batch only. Batch of four images in, one base64 string out. If you need each frame encoded separately, split the batch upstream.
  • mask (optional) - the interesting bit. Give it a mask and the node bakes it into the PNG's alpha channel, producing an RGBA image where the mask is transparency. That's a genuinely useful trick: you can hand an API or a vision model an image and its region mask in one self-contained payload, no sidecar needed. Leave it empty and you get a plain opaque RGB PNG.
  • add_data_prefix (default on) - prepends data:image/png;base64, so the output is a ready-to-use data URI. Turn it off if your consumer wants the raw base64 blob (e.g. it'll add its own prefix).

Mechanically it's straightforward: tensor to numpy, scale 0–255, PNG-encode with Pillow, base64 the bytes. Output is a single STRING.

Where this fits in the ecosystem: the pack's companion node ImageFromBase64 reverses it (and splits the alpha back out as a mask), so this is one half of a base64 round-trip - useful when you're moving images through a string-only channel like an LLM API call or a database. If you're just saving an image to disk, use ComfyUI's own SaveImage; base64 is a transport format, not a storage format, and it's ~33% larger than the raw file. Reaching for this node to "save" an image is the wrong tool.

Worth knowing too: base64 is a string, and a PNG's base64 is a long one. String nodes in ComfyUI are fine with that, but if you're passing it into a node with a small text box, you'll want it wired (forceInput) rather than pasted. Also, because this pack's UI labels are in Chinese and its README is a stub ("some toolkits"), the label fields on the inputs - "上游图像", "上游蒙版", "添加Data前缀" - are the author's own descriptions; they just read as "upstream image", "upstream mask", "add data prefix."

Install

ComfyUI Manager → search comfyui-spawner-nodes → install → restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/spawner1145/comfyui-spawner-nodes

Deps are three pure-Python libs (piexif, pypng, xmltodict) - nothing heavy, no models. The author, spawner1145, is a Chinese-speaking dev also known for a Wan2.1 extension for SD WebUI; this pack reads as his personal toolkit.

Troubleshooting

  • Only the first image of a batch comes out - by design. Split the batch if you need per-frame encoding.
  • Mask looks inverted in the result - the mask becomes alpha, so mask=1 means opaque (visible) and mask=0 means transparent. If your consumer expects the opposite polarity, invert the mask before this node.
  • Output is huge - base64 inflates size by ~33% on top of the PNG. If payload size matters, this isn't the path; save the file and reference it instead.
Categoryspawner/utils

Inputs (3)

NameTypeDefaultDescription
imageIMAGE
maskoptMASK
add_data_prefixoptBOOLEANtrue

Outputs (1)

NameTypeDescription
base64_strSTRING