Nodes/ComfyUI_Swwan/Image Uncrop By Mask (Swwan)
ComfyUI Node

Image Uncrop By Mask (Swwan)

Paste an edited crop back where it came from

By aining2022·Created 10 months ago·Updated about 18 hours ago· 33
Image Uncrop By Mask (Swwan)
  • destination
  • source
  • bbox
  • mask
  • image
◄border_blending0►

What it's for

This is the back half of the crop-and-stitch loop that every serious localised-edit workflow runs: crop a region out of a big image, re-generate or retouch it at proper resolution, then put it back exactly where it was without disturbing the rest of the frame.

The KB's masking doc describes the same loop for automatic detailing - detect a region, crop it, resample, paste back - and this node is the paste-back for when you supply the region instead of a YOLO detector. It's adapted from KJNodes, and it's the third leg of a tripod alongside crop and edit.

How the paste works

You give it three required things:

  • destination - the full original image (or batch) you want the patch applied to.
  • source - the processed crop, of whatever size your editor produced.
  • bbox - the BBOX describing where the crop came from: x0, y0, x1, y1 per source image.

Under the hood the node walks the source images and, for each one, takes bbox[i], LANCZOS-rescales source[i] to the box's width and height, then composites: destination[i] * (1 - mask) + source * mask. Where the mask is 1, you see the new pixels; where it's 0, the original is untouched. The blend is a per-pixel lerp, so a soft mask gives you a soft seam rather than a visible rectangle edge.

The optional mask is that blend weight. Leave it off and the node builds an all-ones mask, i.e. a hard rectangular paste with a visible boundary - often exactly what you want for a clean splice, but not for skin or hair. border_blending is an integer knob the author describes as "Blur the mask edges in the bounding box": it's a gaussian blur applied to the mask with sigma equal to the value, separable into two 1D convs, so 10-ish feathers the join over roughly 30 pixels and 0 is off. Blur too much and the original image bleeds back through the centre of your patch; keep it small.

Wiring it up

bbox is the fiddly part. In this pack the node that emits a real BBOX is Image Crop By Mask And Resize - crop, edit, uncrop, done, with the same tensor flowing through. Outside the pack, Impact Pack's BBOX detector produces the same structure. What does not work is the BOX output from the aspect/resize nodes: the pack's README warns in plain terms that BOX, BBOX, IMAGE_BOUNDS, SEAM and STITCH3 are distinct protocols and cannot be swapped. A BOX won't type-check into this input, and if you force it with a converter you'll get garbage geometry rather than an error.

The single output is image, back at destination resolution, so it slots in wherever the original went.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/aining2022/ComfyUI_Swwan
cd ComfyUI_Swwan
python -m pip install -r requirements.txt

Restart, refresh the browser, search Swwan. Nothing here needs a model, a detector, or Impact Pack installed - the BBOX is just a tensor. That's the appeal of this pack for crop/stitch work: you get the data contracts without the dependency tree.

Common issues

IndexError on bbox[i]. The box list is shorter than the source batch. One entry per source image, and if your editing node collapsed or expanded the batch, the two no longer line up. Slice both sides to matching lengths first.

Sizes are wrong after pasting. The node rescales the source into the box, so a source crop with a different aspect ratio than the box gets stretched. Keep the crop and the box consistent - that's what the crop node's padding/resolution controls are for.

Visible rectangle where the patch joins. Use the mask input rather than relying on an all-ones default, and give border_blending a value of 5–20. If it's still ugly, your crop is too tight; re-crop with more context.

The whole batch got shifted. BBOX coordinates belong to the image they were measured on. If you resized the destination after cropping, every box is now wrong.

CategorySwwan/Advanced/Mask

Inputs (5)

NameTypeDefaultDescription
destinationIMAGE—
sourceIMAGE—
bboxBBOX—
maskoptMASK—
border_blendingoptINT00–1024Blur the mask edges in the bounding box

Outputs (1)

NameTypeDescription
imageIMAGE—