Save StyleGAN Latent to Image Metadata
The face file that remembers itself
- stylegan_image
- stylegan_latent
A StyleGAN PNG from this node is closer to a project file than a picture. The pixels are the render; the latent is the thing that made them, and it's tucked into the PNG's text metadata so LoadStyleGANLatentImg can hand it back byte-exact later. Save a face today, open it in three weeks, keep editing it - or blend it with another saved face - with no seed, no checkpoint hunting, no "which component was that".
Where it really earns its keep is when StyleGAN is the face factory sitting in front of a diffusion pipeline. Faces are the one thing a GAN does faster and cheaper than any diffusion model, and the pack's own FaceID example leans on exactly that: generate a candidate face, then feed the image into IPAdapterUnifiedLoaderFaceID to condition an SDXL generation on that identity. If you're going to produce candidates by the dozen and keep the good ones, you want the latent travelling with the file.
What it does
Inputs are simple: stylegan_image (the IMAGE batch from StyleGANSampler or anywhere else) and stylegan_latent (the matching STYLEGAN_LATENT). For each frame in the batch it writes a PNG into ComfyUI/output/ using filename_prefix (default StyleGAN), with counter padding so you never overwrite a previous save. Two text chunks go in: stylegan_latent, which is the tensor as numpy → zlib → base64, and stylegan_model_file - the checkpoint filename, when the node can work it out.
That "when it can work it out" is the one subtle bit. The pack registers a latent against the sampler's output image tensor by object identity, and SaveStyleGANLatentImg looks that registration up to find the model name. Run the image through anything downstream - an upscale, a crop, a blend - and you're holding a new tensor, the lookup misses, and the file gets the latent but no model stamp. That's deliberate: after a resize, the pixels and the latent may no longer correspond, so claiming a model would be a lie. If you care about model compatibility tracking, save straight off the sampler.
Batch bookkeeping is worth knowing too. Frame i is paired with latent i, clipped to the last latent if the batch is longer. So if you've sliced or reordered images somewhere upstream, the embedded latents belong to different faces than the pixels. Feed this node the sampler output untouched.
The bit that surprises people
You don't actually need this node. The pack patches core SaveImage and PreviewImage so that saving a StyleGANSampler output directly embeds the same metadata, no extra node required. If you're wondering why your normal saved StyleGAN images already load in LoadStyleGANLatentImg - that's it. Look for a StyleGAN: prefix in the server console if the patched save node ever misbehaves and you want to rule it out.
The asymmetry, though: the patched SaveImage respects ComfyUI's --disable-metadata flag, and this node doesn't. SaveStyleGANLatentImg writes the latent unconditionally. If you turned metadata off because you'd rather not ship your pipeline in every file you post, this node is not following that instruction. Given the latent is a full description of how to regenerate the face, that's a privacy decision worth making consciously rather than by accident - and the same goes for the fact that uploading to a host that recompresses will strip it anyway.
Install
Manager → search ComfyUI-StyleGan (publisher dflfd), or:
cd ComfyUI/custom_nodes
git clone https://github.com/dfl/ComfyUI-StyleGan
Restart. Models in ComfyUI/models/stylegan/. This node itself is pure PIL and numpy - it'll save fine even on a machine where the CUDA plugin build is broken, but the images you're saving had to come from a working sampler, so that's academic. The pack's install cost lives in the runtime-compiled extensions: CUDA Toolkit, ninja, MSVC or GCC, and a 30–60s plugin build logged as Setting up PyTorch plugin ... Done. on the first render.
Where people get burned
- "The metadata's missing." You processed the image before saving, so the registry lookup failed - usually that only costs the model filename, but if the pixels came from something that re-encoded them as JPEG/WebP, there's no latent to embed either.
- A PNG that won't load back. Converted, recompressed, or screenshotted at some point. Metadata is a text chunk; anything that rewrites the file drops it.
- Blending two saved files and getting a mess. Different checkpoints. Check the
model_fileoutput ofLoadStyleGANLatentImgon both before you trust the blend. - Prefix collisions. You set
filename_prefixto something you already use for regular saves and now output/ mixes SD and StyleGAN images. Keep it distinct - and rememberLoadStyleGANLatentImgfilters by metadata anyway, so the mess is cosmetic.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| stylegan_image | IMAGE | — | |
| stylegan_latent | STYLEGAN_LATENT | — | |
| filename_prefix | STRING | StyleGAN | — |
Outputs (0)
No outputs