Nodes/ComfyUI-PJLatent/PJ Latent 图像潜空间生成器
ComfyUI Node

PJ Latent 图像潜空间生成器

PJ Latent Generator picks the aspect ratio for you

By pongjoo·Created 8 months ago·Updated 17 days ago· 0
PJ Latent 图像潜空间生成器
    • 潜空间
    • 宽度
    • 高度
    宽高比1:1
    最长边1024
    批次大小1

    Every SD, SDXL, or Flux workflow starts the same boring way: you need an empty latent. It's the blank canvas the diffusion model denoises into a picture, and ComfyUI's built-in EmptyLatentImage makes you type both dimensions yourself - which is exactly how you end up googling "what resolution is 16:9 at 1024 anyway" for the tenth time. PJ Latent Generator is that node but with the math done for you: pick an aspect ratio, set the longest side, and it hands back a correctly-sized, correctly-shaped latent.

    It's a small node in a small pack (pongjoo/ComfyUI-PJLatent, five nodes, MIT licensed), and it does one thing properly rather than a dozen things badly. If you already remember that SDXL wants 832×1216 and Flux wants 1344×768, you can skip it. If you keep rotating through ratios for character sheets, multi-panel grids, or 21:9 ultrawide tests, it earns its place.

    How it works

    The node takes your ratio (say 16:9), decides which side is the long one, and computes the other side so the longest side is exactly what you asked for: height = longest_side * (9 / 16). Then it snaps both dimensions down to a multiple of 8 and creates torch.zeros([batch, 4, H/8, W/8]).

    Those numbers matter. The 4 is the channel count of the SD-class VAE's latent space - SD 1.5, SDXL, and Flux all encode to four latent channels. The /8 is the spatial downsample: the VAE compresses each side by 8x, so a 1024×1024 image lives in a 128×128 latent. Diffusion models choke if your latent dimensions aren't divisible by 8, and this node makes that impossible by construction. That's the whole point of it, and it's the same reason the built-in node fails on you if you type a bad number - this one just refuses to let you.

    Inputs and outputs

    Only three inputs, and you'll mostly touch two:

    • aspect_ratio - a dropdown with 9 presets: 1:1, 4:3, 3:4, 16:9, 9:16, 2:3, 3:2, 21:9, 9:21.
    • longest_side - an integer, default 1024, range 64–8192, steps of 8. This is the dimension that stays fixed; the other is derived.
    • batch_size - how many empty latents to stack, default 1.

    It outputs LATENT (wire that into your KSampler or conditioning), plus width and height as integers. Those extra outputs are quietly handy: you can feed them into a text node to stamp the resolution into your prompt or filename, or use them to set up a matched upscaler downstream.

    Installing it

    The pack installs like any custom node. Easiest is ComfyUI Manager - search for PJLatent and hit install. Otherwise:

    cd ComfyUI/custom_nodes/
    git clone https://github.com/pongjoo/ComfyUI-PJLatent.git
    

    Then restart ComfyUI. The node lives under Add Node → PJ_Nodes → Latent. No model files, no extra Python packages - this node is pure torch and folder_paths, so it's the least fussy thing in the pack.

    Where people get burned

    Two things, neither fatal. First, the max longest_side of 8192 is a fantasy - a 8192×4608 latent is 4 GB of VRAM before the model even starts, and no consumer card is sampling it. Treat 1024–1536 as the real envelope. Second, remember this emits zeroed latents, which is what a fresh text-to-image generation wants. If you're doing img2img, you don't want this node at all - you want to encode an actual image through a VAE. The zero latent is for when there's no starting picture, only a prompt.

    CategoryPJ_Nodes/Latent

    Inputs (3)

    NameTypeDefaultDescription
    宽高比COMBO1:19 options: 1:1, 4:3, 3:4, 16:9, 9:16, 2:3, +3
    最长边INT102464–8192
    批次大小INT11–64

    Outputs (3)

    NameTypeDescription
    潜空间LATENT
    宽度INT
    高度INT