ZML_遮罩裁剪
Crop by mask, with padding, and get the inverse for free
- 图像
- 遮罩
- 裁剪图像
- 反转裁剪
- 反转遮罩
Crop-by-mask is a surprisingly fiddly thing to assemble by hand: cut the image where the mask is, keep the alpha, maybe grow the region a bit so the crop isn't skin-tight, and often you also want the inverse so you can stitch things back. ZML_MaskCropNode (ZML_遮罩裁剪) packages the whole thing into one node and gives you the inverse output as a bonus.
Inputs: 图像 (the IMAGE), 遮罩 (the MASK), 遮罩缩放系数 (mask scale, 0–3, default 1), and 保持原始分辨率 (keep original resolution, default true). The mechanism, in order: it computes the inverse mask (1 - mask) immediately; then per batch image it converts the mask to a PIL L-mode image, and - this is the nice part - if the scale factor isn't 1, it grows or shrinks the mask around its centroid using OpenCV's rotation matrix (cv2.getRotationMatrix2D at the mask's moments centroid). The scaled mask is used as an alpha to paste the source into a transparent RGBA canvas. That gives you the cropped-subject image with a clean alpha channel.
Outputs: 裁剪图像 (the subject on transparency), 反转裁剪 (everything except the mask, also on transparency), and 反转遮罩 (the 1-mask as a MASK, for whatever mask math you want downstream).
The 保持原始分辨率 toggle is where the two workflows diverge. With it on (default), the crops stay at full image size with the mask area showing and everything else transparent - which is what you want when the crop will be composited back at the same coordinates, like in a detail/inpaint loop. With it off, the node runs getbbox() and auto-crops the empty borders, giving you a tight crop of just the subject - which is what you want before an upscale or a dedicated re-render pass. Note that when off, the two image outputs can come out different sizes (the inverse has different empty borders), so don't blindly stack them.
The batch handling is worth knowing: each image in a batch gets its own crop, and if the mask has fewer frames than the image batch, the first mask is reused - so one mask over a whole batch is fine, which is the common case. Where people get burned: an empty mask produces a 1×1 transparent image in tight-crop mode instead of an error, and the scale factor rotates-around-centroid rather than uniformly eroding, so at low factors small or oddly-shaped masks can vanish. For a face-detail crop, 1.0–1.2 is usually right; don't crank it expecting more padding everywhere.
This is the ZML answer to "crop the subject, then fix it, then put it back" - the same loop the KB's masking doc describes as the core of automatic detailing, done manually with one node. Install the pack:
cd ComfyUI/custom_nodes
git clone https://github.com/zml-w/ComfyUI-ZML-Image
or via Manager, restart, and find it under ZML 图像 → 遮罩. It needs opencv-python (in the pack's requirements) for the centroid scaling, so let Manager install requirements or pip install -r requirements.txt on a manual clone.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| 图像 | IMAGE | — | |
| 遮罩 | MASK | — | |
| 遮罩缩放系数 | FLOAT | 1.00-1–3 | — |
| 保持原始分辨率 | BOOLEAN | true | — |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| 裁剪图像 | IMAGE | — |
| 反转裁剪 | IMAGE | — |
| 反转遮罩 | MASK | — |