Match Subject to Reference Image ⚡
When an edit shrinks your subject, scale it back to match
- image
- mask
- reference_image
- reference_mask
- image
- mask
- bbox_data
- info
If you've used an instruction-edit model like Qwen-Image-Edit on a real image, you've hit the subject-size problem: you ask it to change something, and the model quietly redraws your subject a bit smaller or off-center, so when you composite the edit back you get a mismatched layer that doesn't line up. Match Subject to Reference Image is a scale-and-align correction node built for exactly that failure. You feed it the edited image, the reference image (pre-edit, same canvas), and masks for both, and it computes how much the subject's footprint shrank and scales the edit back up so the subject matches the reference's size again - then centers it on the original canvas so you can stitch it down.
It's part of TextureAlchemy's Texture Alchemist/Inpainting group, and its source docstring is refreshingly specific: it exists to fix "pre–Qwen Edit vs post-edit, same canvas but smaller subject." That's a niche, but it's a common niche - and it's the difference between a clean stitch and a misaligned mess.
How it works
The node finds the bounding box of your subject in both the current image and the reference (using each image's mask), then compares them. match_mode decides how the scale factor is derived:
- larger_side - scale so the subject's larger side matches the reference's larger side. The default, and the one that fixes "edit shrank the subject."
- smaller_side - match on the smaller side instead.
- width / height - match only along one axis.
- area - match by total footprint (square-root of the area ratio).
It then uniformly scales the current image by that factor, and centers the scaled subject back onto an output canvas the same size as the original, using the scaled subject's centroid so the result lands where it should. Outputs are the scaled-and-centered image, its mask (scaled the same way, so they stay aligned), a bbox_data object, and an info STRING that records the math - canvas size, scale factor, and the before/after subject dimensions. That info string is genuinely useful for debugging why a result is off.
padding adds margin around the subject bbox on both images, and invert_mask flips both masks if your masks are background-selected rather than subject-selected.
Where it fits
The intended flow is: reference image + mask → run the edit → feed the edited result, the reference, and both masks into this node → take the output image and stitch it onto the original (Inpaint Stitcher works fine here). If the edit model also drifted content, this won't fix content drift - it fixes scale and centering, which is the mechanical part. If you only need scale with no centering, the node's sibling ScaleImageToReferenceBbox in the same file is the lighter variant.
Installing it
Ships with ComfyUI-TextureAlchemy. ComfyUI Manager → search "TextureAlchemy" → install → restart, or:
cd ComfyUI/custom_nodes
git clone https://github.com/amtarr/ComfyUI-TextureAlchemy
Restart, find it under Texture Alchemist → Inpainting. No pip packages or model downloads - pure tensor resampling.
Common issues
- "It did nothing / scale stayed 1.0." Check the console message "Empty mask on reference or current image" - if either mask is empty or doesn't cover the subject, the node bails to a no-op. Fix the masks.
- "The result is the wrong size overall." It scales the whole image, not just the subject, then crops/centers to the canvas. If your edit changed the canvas dimensions, resample the reference or current image to a common size first (the node notes when the reference size differs).
- "The subject is scaled right but positioned wrong." Centering is based on the scaled subject centroid - if the edit moved the subject sideways, the mask comparison may not reflect where you want it. Adjust or accept that drift.
It's a fix-it node for a specific failure, and it does that one thing well. If you composite edited images back onto originals on a regular basis, you'll wonder how you lived without it.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| mask | MASK | — | |
| reference_image | IMAGE | — | |
| reference_mask | MASK | — | |
| match_mode | COMBO | larger_side | How to pick S from ref vs current subject bbox (same as Scale Image to Reference Bbox). larger_side: grow subject to match ref (typical after edit shrinks it) |
| padding | INT | 00–500 | Padding around mask bbox on both images (pixels) |
| invert_mask | BOOLEAN | false | Invert both masks before bbox detection |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | — |
| mask | MASK | — |
| bbox_data | BBOX_DATA | — |
| info | STRING | — |