Powerful Mask Crop
The crop half of a paste-back loop that remembers where it cut
- image
- mask
- cropped_image
- cropped_mask
- crop_region_mask
- crop_info
Every local-edit workflow in ComfyUI is the same four steps dressed up differently: cut a region out, fix it at a resolution the model can actually work with, paste it back, and make the seam disappear. This node is step one, done properly. It takes an image and a mask, crops to the mask's bounding box, and - the part that saves you - writes down everything it did so a matching restore node can undo it later. You never hand-type coordinates into a Crop widget again.
Powerful Mask Crop is one of five nodes in the flywhale-666/ComfyUI_pixel_snapping pack, and it's the front half of a crop → process → restore loop. The back half is its sibling Powerful Mask Restore, which consumes the crop_info output and does the pasting. Think of it as the Inpaint Crop half of the "Inpaint Crop and Stitch" pattern, but with nicer padding control and an explicit resize step for feeding a fixed-size face into a model.
How it works
The mechanism is straightforward geometry. It finds the bounding box of every mask pixel above 0.5, adds your per-side padding (clamped to the image edges), and crops. Then, if you enable enable_resize, it adjusts the crop to hit a target aspect ratio - by extending the crop region when there's room, or padding with black or white when there isn't - and scales it to target_width × target_height. The size_multiple value (default 16) rounds the final dimensions up to a multiple, which matters because most diffusion models are happiest at resolutions divisible by 8 or 16.
Everything it does is recorded in the crop_info output: the original image size, the exact crop rectangle, the cropped size, and the padding that was added. That dictionary is the contract with the restore node - no guessing, no re-deriving coordinates.
The inputs that matter
imageandmask- the raw material. White in the mask is what gets cropped around.top_padding/bottom_padding/left_padding/right_padding- separate margin per side, defaults of 30px. This is how you give a face crop room for hair, or a subject room to breathe.enable_resize,target_width,target_height- set a fixed output size (0 means "derive from the other dimension"). This is the "feed it to a 1024×1024 face model" switch.padding_mode-blackorwhitepad the crop to reach the target aspect ratio;noneletterboxes instead, keeping the original aspect ratio and extending the crop only where the image has room.size_multiple- leave it at 16 unless you know why you're changing it.
Outputs
cropped_image- the crop, resized if enabled.cropped_mask- the mask, cropped and resized to match.crop_region_mask- a rectangle at full original resolution marking where the crop was taken. Handy if you want to visualize or gate downstream steps.crop_info- the CROP_INFO dict. This is the one that feeds Powerful Mask Restore.
Install
Same story as the rest of the pack. ComfyUI Manager, search "Pixel Snapping", or:
cd ComfyUI/custom_nodes
git clone https://github.com/flywhale-666/ComfyUI_pixel_snapping
cd ComfyUI_pixel_snapping
pip install -r requirements.txt
Restart ComfyUI fully afterward - the README warns that port changes from this pack don't apply on a page refresh. Dependencies are just OpenCV, numpy, torch and ultralytics, which you almost certainly already have.
Common issues
- Empty mask - if the mask is all zeros you get the original image back, which is probably not what you wanted. Check that white in your mask means the region you intend.
- White edges later - if you use
padding_mode: whiteand then composite the crop yourself (bypassing the restore node), you'll paste white bars into the final image. The restore node strips padding usingcrop_info; your own compositor won't know to. - Batch gotcha - like the rest of this pack, only the first image of a batch is processed. Batch workflows need to loop.
It's a small utility with one genuine superpower: it makes the crop step reversible. Combined with its restore sibling, you get a local-edit loop that doesn't smear the original through a VAE encode/decode cycle - only the pixels you meant to touch ever get re-rendered.
Inputs (11)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| mask | MASK | — | |
| top_padding | INT | 300–10000 | — |
| bottom_padding | INT | 300–10000 | — |
| left_padding | INT | 300–10000 | — |
| right_padding | INT | 300–10000 | — |
| enable_resize | BOOLEAN | false | — |
| target_width | INT | 00–16384 | — |
| target_height | INT | 00–16384 | — |
| padding_mode | COMBO | 3 options: black, white, none | |
| size_multiple | INT | 161–999 | — |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| cropped_image | IMAGE | — |
| cropped_mask | MASK | — |
| crop_region_mask | MASK | — |
| crop_info | CROP_INFO | — |