Nodes/ComfyUI_Swwan/RestoreCropBox (Swwan) · 旧版兼容
ComfyUI Node

RestoreCropBox (Swwan) · 旧版兼容

Put the repaired patch back exactly where it came from

By aining2022·Created 10 months ago·Updated about 18 hours ago· 33
RestoreCropBox (Swwan) · 旧版兼容
  • background_image
  • croped_image
  • crop_box
  • croped_mask
  • image
  • mask
◄invert_maskfalse►

Crop a face out at 4x, fix it, paste it back. That's the whole job, and it's the job almost every "detail the small thing" pipeline in ComfyUI quietly depends on: the detector finds the region, the crop node cuts it out, you upscale and re-diffuse just that patch, and something has to put the patch back - at the same pixel coordinates, without you hand-aligning anything.

This node is that last step. One honest warning before you wire it: it's the legacy version. The pack marks it RestoreCropBox (Swwan) · 旧版兼容 and points you at SwwanRestoreCropBoxV4, which is the same idea rewritten in PyTorch with feathering, a GPU path and seam handling. Use V4 for new graphs. You reach for this one because a workflow already contains it - including workflows that used to run LayerStyle's LayerUtility: RestoreCropBox, because that interface is exactly what this is.

How it works

Mechanism first, because it explains every gotcha below. The node takes your original background_image, copies it, and pastes croped_image onto that copy at box=tuple(crop_box) - the same BOX rectangle the crop node produced upstream. The paste uses a mask, and where that mask comes from is the whole trick:

  • if you wired croped_mask, that mask is used (inverted first when invert_mask is on);
  • if you didn't, it falls back to the cropped image's own alpha channel if it has one;
  • and if there's no alpha either, it makes a solid white mask - a hard rectangular paste of the entire patch.

It also builds a second image, a black canvas with your mask stamped into it at the same box, and returns that as mask. That's the region you just edited, as a mask, for whatever comes next (a blend, a colour match, a second pass).

Batching is permissive in a way worth knowing about: it runs for as many frames as the longest input, and repeats the last image or mask to fill any shorter list. Two frames in, one crop out? Frame two gets frame two's background and the crop again.

Inputs and outputs

background_image (IMAGE) - what you cropped from. croped_image (IMAGE) - the edited patch. crop_box (BOX) - the rectangle from the crop node; the pack's own migration notes list BOX as "the shared crop rectangle used by this repo's legacy Crop/Restore protocol", which is a polite way of saying it is not interchangeable with WAS's BBOX or KJ's IMAGE_BOUNDS. invert_mask (BOOLEAN) and the optional croped_mask (MASK) round it out. Outputs are image and mask, both single-batch.

Install

This ships in aining2022/ComfyUI_Swwan, so you don't install anything for this node alone. ComfyUI Manager → search for the pack title, or:

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

The requirements are light - numpy, Pillow, opencv-python, scipy, scikit-image, with torch/torchvision taken from your ComfyUI environment (Python 3.11+). Restart ComfyUI and refresh the browser; search for Swwan.

Where people get burned

Hard edges. This version pastes without any feathering. On a high-contrast edit you'll see a visible rectangle. Either move to SwwanRestoreCropBoxV4, which adds feathering and seam handling, or blur the mask before it gets here.

The alpha you have is not the alpha you think. ComfyUI's Load Image hands you MASK as 1 - alpha; this pack's RGBA path gives you the actual alpha. Feed the wrong one and your paste is inverted - everything except the patch gets replaced. That's what invert_mask is for.

Boxes don't cross packs. Wiring a BBOX or IMAGE_BOUNDS into crop_box fails validation, and a stale box from a different pipeline can land silently off-position.

Pack-level conflicts are a real report. Users on r/comfyui have described missing-node warnings and widget weirdness attributed to ComfyUI_Swwan sitting alongside other packs (one report was a Kija/KJNodes workflow, another a Z-Image workflow whose style list broke until Swwan was removed). If a different pack's nodes go strange right after you install this, that's worth testing by disabling one of them.

If you're moving an old graph onto these nodes, the repo ships a migrator - dry-run it first:

python scripts/migrate_workflow.py old.json --dry-run
python scripts/migrate_workflow.py old.json --swwan-node-id 12 --comfyui-root /path/to/ComfyUI

Same display name does not mean same node ID. There's a table in docs/NODE_CATALOG.md, and it's the fastest way to find out what your old node became.

CategorySwwan/Legacy

Inputs (5)

NameTypeDefaultDescription
background_imageIMAGE—
croped_imageIMAGE—
invert_maskBOOLEANfalse—
crop_boxBOX—
croped_maskoptMASK—

Outputs (2)

NameTypeDescription
imageIMAGE—
maskMASK—