Nodes/ComfyUI-StyleGan/Load StyleGAN Latent from Image Metadata
ComfyUI Node

Load StyleGAN Latent from Image Metadata

Get the exact face back from a PNG

By dfl·Created 2 years ago·Updated 18 days ago· 0
Load StyleGAN Latent from Image Metadata
    • image
    • stylegan_latent
    • model_file
    ◄stylegan_image▾►

    StyleGAN images are cheap and deterministic - same seed, same face, forever. So why would you need this node? Because seeds are only portable if you also remember the checkpoint, the truncation, and whether you had moved the latent along a direction before rendering. The seed in your notes doesn't reproduce a face that was actually seed + 1.4 × component_02.

    This node sidesteps all of that. Any StyleGAN image saved by this pack carries its exact latent in the PNG's text metadata. Load the file and you get the latent back - not an approximation, not an inversion, the actual [1, num_ws, w_dim] tensor that produced those pixels. Which means you can blend two saved faces, keep editing one you made last week, or re-render it as many times as you like without ever knowing how you got there.

    How it works

    The pack writes the latent into a PNG text chunk called stylegan_latent - the array serialized with numpy, zlib-compressed, base64-encoded. Reading it back is the reverse: decode, decompress, unpickle-free np.load, send to your compute device. There's a second chunk, stylegan_model_file, holding the checkpoint filename. Note that this is metadata in the same PNG chunk family ComfyUI already uses for workflows, and it's just as fragile: re-encode the image, upload it somewhere that recompresses, or convert it to JPEG, and the latent is gone. The pixels survive; the tensor doesn't.

    The file dropdown is smarter than it looks. It lists images from input/, output/ and temp/, and it filters to only those whose PNG metadata actually contains a StyleGAN latent - it opens each candidate and checks, which is cheap because PIL reads .info without decoding pixel data. Your output folder full of unrelated SD generations won't clutter it.

    Three outputs:

    • image - the pixels as a normal ComfyUI IMAGE, decoded 0–1 float RGB at native resolution. Wire it into anything: an upscaler, FaceID, a save node.
    • stylegan_latent - the latent itself, for ApplyStyleGANDirection, StyleGANDirectionSweep, StyleGANSampler, or BlendStyleGANLatents.
    • model_file - a STRING with the checkpoint that made it, e.g. BroGANv1.2.0.safetensors. This is the quiet hero of the three. Before you blend two faces, check that both images report the same model. Latents from different checkpoints aren't guaranteed compatible, and nothing else in the graph will warn you.

    The loader also registers the latent against the image tensor it just produced, so if you save that image again through core SaveImage, the metadata rides along.

    Having both halves of the same file is more useful than it sounds. StyleGAN generates a usable face in milliseconds, which makes it a cheap identity source for the FaceID/InstantID side of a diffusion pipeline - and if the face you liked was itself generated with a direction applied, this is the only way back to it.

    Getting the workflow to actually run

    Two things bite people on the pack's own example: the workflow references face_A.png and face_B.png, which live in examples/. Copy them into ComfyUI/input/ first - unlike a seed-based mixer, those are specific files and not portable on their own. And if you're picking from the output gallery, the node falls back to searching output/ and temp/ by basename, so a file you just generated is fine.

    Install

    Manager → search ComfyUI-StyleGan (publisher dflfd), or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/dfl/ComfyUI-StyleGan
    

    Restart. Models go in ComfyUI/models/stylegan/. This node doesn't render, so it doesn't need the compiled CUDA kernels - but whatever you wire its latent into probably does, and that's the part of this pack's install that hurts (CUDA Toolkit, ninja, MSVC/GCC, 30–60s plugin build on first render).

    Where people get burned

    • "has no stylegan_latent metadata" - the file wasn't produced by a StyleGAN sampler output saved through SaveImage, PreviewImage, or SaveStyleGANLatentImg. Most often it came from somewhere that recompressed it, or it's a plain photo. For a plain photo the pack's own answer is StyleGANInversion, which approximates a latent by optimisation.
    • The image isn't in the dropdown at all. Same root cause, but here the filter is up front: if the picker doesn't show it, the latent chunk isn't there.
    • Blending two faces and getting a horror show. Check model_file on both first. Different checkpoints, incompatible latents.
    • You rendered at a different resolution than the file. The image output is the saved pixels; StyleGANSampler re-synthesises from the latent at the model's native size. They're the same face, not the same file.
    CategoryStyleGAN

    Inputs (1)

    NameTypeDefaultDescription
    stylegan_imageCOMBO0 options:

    Outputs (3)

    NameTypeDescription
    imageIMAGE—
    stylegan_latentSTYLEGAN_LATENT—
    model_fileSTRING—