๐ฆ RS Image to Latent
Image to latent without the 'size not divisible' error โ VAE-aware and resizable
- image
- vae
- latent
- width_px
- height_px
- width_latent
- height_latent
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 withRSCropImage'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.
Inputs (12)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | Input image to convert to latent | |
| vae | VAE | VAE model for encoding (auto-detects divisibility) | |
| mode | COMBO | auto | Size selection mode: - auto: Preserve image size with minimal rounding - preset: Use predefined resolution - custom: Manually enter width/height - megapixels: Set target megapixels |
| preset | COMBO | Square - 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 |
| width | INT | 102464โ4096 | โ |
| height | INT | 102464โ4096 | โ |
| megapixels | FLOAT | 1.00.1โ16 | โ |
| aspect_ratio | COMBO | 1:1 | 9 options: 1:1, 3:2, 2:3, 4:3, 3:4, 16:9, +3 |
| divisibility_mode | COMBO | auto | Divisibility requirement (auto = detect from VAE) |
| upscale_method | COMBO | lanczos | 4 options: lanczos, bilinear, nearest-exact, bicubic |
| rounding_mode | COMBO | auto | How to adjust size for divisibility: - auto: pick nearest valid size - shrink: only reduce (never increase) - expand: only increase (never reduce) |
| batch_size | INT | 11โ64 | Number of identical latents in batch (useful for batch processing) |
Outputs (5)
| Name | Type | Description |
|---|---|---|
| latent | LATENT | โ |
| width_px | INT | โ |
| height_px | INT | โ |
| width_latent | INT | โ |
| height_latent | INT | โ |