Qwen Image Edit Latent Krs
Build the latent yourself — fine-grained resolution control for Qwen Edit samplers
- image
- model
- vae
- preview
- latent
- viz_image
- padded_image
Normally you don't build a latent by hand - you let ComfyUI's VAE encode do it, and you take whatever resolution falls out. QwenImageEditLatentKrs is the exception, and the author is upfront that it's experimental. It takes your image and hands you back a latent at a resolution you choose, snapped to values that won't break the VAE math, plus the padded image and a status readout. It's for when the standard Qwen-Edit nodes don't give you the output size you want.
What it actually does
Give it an image and a target size, and it does three things in sequence:
- Snaps your target dimensions to something valid - multiples of 16, 32, or 64, or powers of 2 divisible by 16 - so the latent divides cleanly (
latent_scale8 or 16).locked_axisto "width" or "height" and the other dimension is derived from your image's aspect ratio instead of drifting; "independent" lets both go separately. Want a 1024×1152 outpaint from a square input without distorting it? This is how. - Fits the image to the snapped canvas -
pad_onlycenters it on a black/gray/white background,allow_cropcenter-crops instead. - Builds the latent - by default a zeros or random-noise tensor at the right size (
latent_mode, withlatent_seedfor reproducibility). But connect a vae and it switches to encoding the padded image for real, which is the path you actually want for Qwen: the sampler then has a proper reference latent and a controlled resolution.modelauto-detects latent channels so you don't have to guess.
Inputs a beginner sets
- width_latent / height_latent - what you want; the node fixes them up.
- vae - connect it. Without it you get a noise/zero latent, which is a niche trick; with it, a real latent.
- fit_mode -
pad_onlykeeps every pixel (new space is padding),allow_croptrims to match. - target_megapixels - set
use_target_megapixelsand it targets total pixels instead of exact dimensions, nice for staying inside a VRAM budget.
The outputs are the interesting part: latent (into a KSampler), padded_image (the fitted canvas), viz_image - a diagram with your reference box and output box drawn to scale, so you can see exactly what got padded - and preview, a plain-string status report of everything it did. String outputs don't render in the graph, so preview is there to read in the console, but it's a genuinely complete record of the snap decisions.
Where it fits (and where it doesn't)
This node exists because the Qwen offset story is partly a resolution story: control the input size and the output behaves. Feeding Qwen-Edit through this node's VAE path gives you that control at the latent level - pad to your target, encode, sample - which is useful for outpainting or for hitting a resolution the native text-encode nodes don't offer. Note the KB's caution that Qwen re-emits the whole frame, so a custom latent size doesn't buy you pixel-exact preservation; if that's the goal, masked inpainting is still the answer.
Installation
cd ComfyUI/custom_nodes
git clone https://github.com/KursatAs/KrsNodes
Restart, or search "KrsNodes" in ComfyUI Manager. No extra dependencies, no model downloads in the pack - but you're feeding a 20B Qwen model, so realistically you're on a GGUF quant and want the megapixel target to respect your VRAM.
Gotchas
- It's flagged experimental by its own author. Great for learning what the snap/encode pipeline does; don't build your whole production workflow on it until you've eyeballed a few outputs.
- The
latentoutput is a custom tensor, not the standard VAE encode of your original image. If your sampler behaves oddly, checkpreviewin the console - it tells you exactly what it snapped and whether the VAE path actually ran. latent_scaleis 8 for SD1.5-style latents and 16 for SDXL; for Qwen you want the VAE to be the source of truth, so leave the defaults and connect the model.
Inputs (22)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | The input image tensor to snap dimensions and process. | |
| width_latent | INT | 102416–4096 | Desired output width in pixels. Snapped to valid values based on snap_mode. |
| height_latent | INT | 102416–4096 | Desired output height in pixels. Snapped to valid values based on snap_mode. |
| locked_axis | COMBO | width | Which axis to lock when snapping dimensions. 'independent' allows both to snap separately. |
| snap_mode | COMBO | multiple_of_16 | How to snap dimensions: multiples of 16, 32, 64, or powers of 2 divisible by 16. |
| use_exact_input_dims | BOOLEAN | false | If enabled, use exact input dimensions without snapping. |
| use_target_megapixels | BOOLEAN | false | If enabled, snap dimensions to match the target megapixels instead of user width/height. |
| target_megapixels | FLOAT | 1.000.1–8 | Target total megapixels for the output image when use_target_megapixels is enabled. |
| fit_mode | COMBO | pad_only | How to fit the image: pad only (no cropping) or allow cropping to match snapped dimensions. |
| snap_strategy | COMBO | nearest | Strategy for snapping: round up, down, or to nearest valid value. |
| background_color | COMBO | gray | Background color for padding areas: black, gray, or white. |
| latent_scale | COMBO | 8 | Downscale factor for latent space, typically 8 for SD1.5 or 16 for SDXL. |
| latent_channels | INT | 41–32 | Number of channels in the latent tensor. Auto-detected from model if provided. |
| latent_mode | COMBO | zeros | How to initialize the custom latent: all zeros or random noise. |
| latent_seed | INT | -1-1–2147483647 | Seed for random latent generation. -1 for random seed. |
| upscale_method | COMBO | lanczos | Interpolation method for upscaling the image. |
| crop | COMBO | center | Cropping method during upscaling: disabled or center crop. |
| force_5d | BOOLEAN | false | Force 5D tensor for VAE encoding (for specific models like SVD). |
| normalize_mode | COMBO | none | Normalization mode for VAE input: [-1,1], [0,1], or none. |
| pad_plus_2 | BOOLEAN | false | Add 2 pixels padding to VAE input (for specific requirements). |
| modelopt | MODEL | Optional model to auto-detect latent channels from. | |
| vaeopt | VAE | Optional VAE for encoding the image into latent space. If provided, switches output to VAE latent. |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| preview | STRING | — |
| latent | LATENT | — |
| viz_image | IMAGE | — |
| padded_image | IMAGE | — |