😺NKD Klein Reference Fit
Make the whole reference land inside your masked zone
- image
- mask
- image
Regional reference control on Klein has a subtle trap: the model lays every reference over the whole canvas, so a masked zone only ever sees the slice of the reference that happens to overlap it. Mask a zone on the left, feed a reference of a product, and you'll get the part of the product that's positionally over that zone - probably not the whole product, and possibly not even the right part. NKD Klein Reference Fit is the fix: it scales the reference to fit your mask's bounding box and places it there on a canvas-sized image, so the entire reference lands inside the zone, aligned with it.
It's an experimental node from the NKD Klein Tools pack, designed to be paired with NKD Klein Reference Region - Fit gets the reference into the zone, Region keeps its influence in the zone.
How it works
Fit reads the mask's bounding box (the tight rectangle around the painted area), optionally grows it by padding pixels, scales your reference to fit inside that box, and pastes it onto a neutral canvas the same size as the mask. The fit mode decides the scaling:
- contain (default) - scales the whole reference to fit inside the zone while keeping its proportions. You see all of it, but there may be a neutral border left over inside the zone.
- stretch - fills the zone exactly, distorting the reference if the shapes don't match.
You control what fills the space around the placed reference with background - gray (neutral, default), black, or white. Gray is mostly a formality: outside the zone the Region node suppresses the background anyway, but it shows in any border inside the zone when you use contain.
If the mask is empty or all black, the node returns the reference unchanged rather than erroring.
Inputs and output
- image - the reference image to place inside the masked region.
- mask - the zone. Its bounding box sets the position and size; its resolution sets the canvas size. Use the same mask as in the Reference Region node - that's what keeps everything aligned.
- fit - contain or stretch.
- padding - grow the bounding box by this many pixels before placing (0–512). Handy when you want a little breathing room around the reference inside the zone.
- background - gray / black / white fill around the placed reference.
Output is a single image - the canvas-sized result - which feeds Presampling's reference slot:
reference image → NKD Klein Reference Fit (with your mask) → Presampling's ref slot
your mask → NKD Klein Reference Region → [your sampler] → NKD Klein Postsampling
That's the whole loop: Fit positions the reference content inside the zone; Region confines its influence there. Without Fit, only the overlapping slice of the reference reaches the zone.
Installing
Search NKD Klein Tools in ComfyUI Manager, or clone:
cd ComfyUI/custom_nodes
git clone https://github.com/Nekodificador/ComfyUI-NKD-Klein-Tools
Restart ComfyUI. No pip dependencies - the pack's requirements.txt is empty (torch and the ComfyUI API come from ComfyUI itself). You supply the Flux Klein model; the pack adds no weights.
Gotchas
- The mask resolution sets the canvas size. Fit produces an image at the mask's resolution, so if your mask is tiny, the reference gets placed on a tiny canvas. Feed it at the resolution Presampling actually expects.
- Don't stretch unless you have to.
containkeeps the reference's proportions intact;stretchfills the zone exactly but can distort the subject. Start with contain. - Pair it with Region. Fit alone just repositions the reference on the canvas; it's the Region node's attention bias that stops it from leaking everywhere. They're meant to be used together, with identical masks.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | The reference image to place inside the masked region. | |
| mask | MASK | The zone to place the reference in. Its bounding box sets the position and size; its resolution sets the canvas size. Use the same mask as the Reference Region node. | |
| fit | COMBO | contain | contain: scale the whole reference to fit inside the zone (keeps its proportions, may leave a neutral border). stretch: fill the zone exactly (may distort). |
| padding | INT | 00–512 | Grow the bounding box by this many pixels before placing. |
| background | COMBO | gray | What fills the area around the placed reference. Gray is neutral. Mostly irrelevant outside the zone (the Region node suppresses it there), but it shows in any border inside the zone when using 'contain'. |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | Canvas-sized image with the reference placed in the zone. Feed into Presampling's reference slot. |