Nodes/ComfyUI_RaykoStudio/๐ŸฆŠ RS Image to Latent
ComfyUI Node

๐ŸฆŠ RS Image to Latent

Image to latent without the 'size not divisible' error โ€” VAE-aware and resizable

By RaykosanยทCreated about a year agoยทUpdated 5 days agoยท 79
๐ŸฆŠ RS Image to Latent
  • image
  • vae
  • latent
  • width_px
  • height_px
  • width_latent
  • height_latent
โ—„modeautoโ–บ
โ—„presetSquare - 1024x1024 (1:1)โ–บ
โ—„width1024โ–บ
โ—„height1024โ–บ
โ—„megapixels1.0โ–บ
โ—„aspect_ratio1:1โ–บ
โ—„divisibility_modeautoโ–บ
โ—„upscale_methodlanczosโ–บ
โ—„rounding_modeautoโ–บ
โ—„batch_size1โ–บ

VAEEncode works, until it doesn't: your image is 1037x1091, the VAE wants multiples of 8, and you get a tensor that's been silently padded or an outright error. ๐ŸฆŠ RS Image to Latent (RS_ImageToLatent) encodes an image into latent space and fixes the size problem in the same step. It detects what the VAE actually needs, rounds your image to a valid size, and hands you the latent plus the exact pixel dimensions it used. For img2img and inpainting workflows, that removes a whole class of fiddly pre-processing.

How it works

The pipeline is: read the image โ†’ decide the target size โ†’ resize if needed (via comfy.utils.common_upscale) โ†’ vae.encode() โ†’ return. The size logic is the interesting part. divisibility_mode defaults to auto, which inspects the VAE's actual downscale factor instead of assuming 8 - so an SDXL VAE gets 8, a Flux VAE gets its real divisor, and so on. rounding_mode controls how the rounding happens: auto picks the nearest valid size, shrink only ever reduces (never upscales - good for keeping VRAM low), and expand only increases. mode then governs the whole target:

  • auto - preserve the original aspect and size, just round to the required multiple.
  • preset - 50+ common resolutions (square, portrait, landscape).
  • custom - your own width/height (64โ€“4096, step 8).
  • megapixels - a target like 1.5 MP plus an aspect_ratio, useful for staying inside VRAM budgets.

Inputs that matter

  • image, vae - the obvious two. Tooltip on vae: "VAE model for encoding (auto-detects divisibility)".
  • mode - auto / preset / custom / megapixels.
  • divisibility_mode - auto, or force 8/16/32/64/112/128.
  • rounding_mode - auto / shrink / expand.
  • upscale_method - lanczos (default), bilinear, nearest-exact, bicubic.
  • batch_size - repeat the same latent N times.

Outputs: latent, plus width_px, height_px, width_latent (width/8), and height_latent (height/8) - the latter two are handy for downstream sizing math.

How to install

Part of ComfyUI_RaykoStudio:

cd ComfyUI/custom_nodes
git clone https://github.com/Raykosan/ComfyUI_RaykoStudio.git

Restart ComfyUI, or install via ComfyUI Manager (search "ComfyUI_RaykoStudio"). Uses only torch and the pack's standard deps.

Common issues

  • Image came back slightly resized when I wanted it identical. That's rounding_mode = auto doing its job - your source simply wasn't a multiple of the VAE's divisor. Set it to shrink to guarantee no upscaling, or pre-crop with RSCropImage's multiple mode to keep control of the exact pixels.
  • Megapixels mode output looks off-aspect. The aspect ratio list is fixed; if your composition doesn't fit one, use preset or custom instead.
  • VAE encode/decode drift. Encoding is lossy - every encode/decode cycle costs a little sharpness (the KB's VAE section covers this). Don't chain encodeโ†’sampleโ†’decodeโ†’re-encode more than you need to.

The honest take: VAEEncode is fine if your images are already clean multiples. This node earns its keep the moment you're feeding odd-sized crops, phone screenshots, or anything from a batch selector into a model with strict requirements - it's the size-multiple problem solved at the right layer.

Category๐ŸฆŠ RaykoStudio

Inputs (12)

NameTypeDefaultDescription
imageIMAGEInput image to convert to latent
vaeVAEVAE model for encoding (auto-detects divisibility)
modeCOMBOautoSize selection mode: - auto: Preserve image size with minimal rounding - preset: Use predefined resolution - custom: Manually enter width/height - megapixels: Set target megapixels
presetCOMBOSquare - 1024x1024 (1:1)54 options: Square - 512x512 (1:1), Square - 640x640 (1:1), Square - 768x768 (1:1), Square - 1024x1024 (1:1), Square - 1280x1280 (1:1), Square - 1536x1536 (1:1), +48
widthINT102464โ€“4096โ€”
heightINT102464โ€“4096โ€”
megapixelsFLOAT1.00.1โ€“16โ€”
aspect_ratioCOMBO1:19 options: 1:1, 3:2, 2:3, 4:3, 3:4, 16:9, +3
divisibility_modeCOMBOautoDivisibility requirement (auto = detect from VAE)
upscale_methodCOMBOlanczos4 options: lanczos, bilinear, nearest-exact, bicubic
rounding_modeCOMBOautoHow to adjust size for divisibility: - auto: pick nearest valid size - shrink: only reduce (never increase) - expand: only increase (never reduce)
batch_sizeINT11โ€“64Number of identical latents in batch (useful for batch processing)

Outputs (5)

NameTypeDescription
latentLATENTโ€”
width_pxINTโ€”
height_pxINTโ€”
width_latentINTโ€”
height_latentINTโ€”