Nodes/Comfyui Krs Nodes/Qwen Image Edit Latent Krs
ComfyUI Node

Qwen Image Edit Latent Krs

Build the latent yourself — fine-grained resolution control for Qwen Edit samplers

By KursatAs·Created 10 months ago·Updated 10 months ago· 0
Qwen Image Edit Latent Krs
  • image
  • model
  • vae
  • preview
  • latent
  • viz_image
  • padded_image
width_latent1024
height_latent1024
locked_axiswidth
snap_modemultiple_of_16
use_exact_input_dimsfalse
use_target_megapixelsfalse
target_megapixels1.00
fit_modepad_only
snap_strategynearest
background_colorgray
latent_scale8
latent_channels4
latent_modezeros
latent_seed-1
upscale_methodlanczos
cropcenter
force_5dfalse
normalize_modenone
pad_plus_2false

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:

  1. 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_scale 8 or 16). locked_axis to "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.
  2. Fits the image to the snapped canvas - pad_only centers it on a black/gray/white background, allow_crop center-crops instead.
  3. Builds the latent - by default a zeros or random-noise tensor at the right size (latent_mode, with latent_seed for 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. model auto-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_only keeps every pixel (new space is padding), allow_crop trims to match.
  • target_megapixels - set use_target_megapixels and 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 latent output is a custom tensor, not the standard VAE encode of your original image. If your sampler behaves oddly, check preview in the console - it tells you exactly what it snapped and whether the VAE path actually ran.
  • latent_scale is 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.
Categoryutils

Inputs (22)

NameTypeDefaultDescription
imageIMAGEThe input image tensor to snap dimensions and process.
width_latentINT102416–4096Desired output width in pixels. Snapped to valid values based on snap_mode.
height_latentINT102416–4096Desired output height in pixels. Snapped to valid values based on snap_mode.
locked_axisCOMBOwidthWhich axis to lock when snapping dimensions. 'independent' allows both to snap separately.
snap_modeCOMBOmultiple_of_16How to snap dimensions: multiples of 16, 32, 64, or powers of 2 divisible by 16.
use_exact_input_dimsBOOLEANfalseIf enabled, use exact input dimensions without snapping.
use_target_megapixelsBOOLEANfalseIf enabled, snap dimensions to match the target megapixels instead of user width/height.
target_megapixelsFLOAT1.000.1–8Target total megapixels for the output image when use_target_megapixels is enabled.
fit_modeCOMBOpad_onlyHow to fit the image: pad only (no cropping) or allow cropping to match snapped dimensions.
snap_strategyCOMBOnearestStrategy for snapping: round up, down, or to nearest valid value.
background_colorCOMBOgrayBackground color for padding areas: black, gray, or white.
latent_scaleCOMBO8Downscale factor for latent space, typically 8 for SD1.5 or 16 for SDXL.
latent_channelsINT41–32Number of channels in the latent tensor. Auto-detected from model if provided.
latent_modeCOMBOzerosHow to initialize the custom latent: all zeros or random noise.
latent_seedINT-1-1–2147483647Seed for random latent generation. -1 for random seed.
upscale_methodCOMBOlanczosInterpolation method for upscaling the image.
cropCOMBOcenterCropping method during upscaling: disabled or center crop.
force_5dBOOLEANfalseForce 5D tensor for VAE encoding (for specific models like SVD).
normalize_modeCOMBOnoneNormalization mode for VAE input: [-1,1], [0,1], or none.
pad_plus_2BOOLEANfalseAdd 2 pixels padding to VAE input (for specific requirements).
modeloptMODELOptional model to auto-detect latent channels from.
vaeoptVAEOptional VAE for encoding the image into latent space. If provided, switches output to VAE latent.

Outputs (4)

NameTypeDescription
previewSTRING
latentLATENT
viz_imageIMAGE
padded_imageIMAGE