Nodes/ComfyUI-WJNodes/mask crop square
ComfyUI Node

mask crop square

Crop to a mask, work at full res, paste it back

By 807502278·Created 2 years ago·Updated 11 months ago· 21
mask crop square
  • images
  • masks
  • crop_data
  • option
  • crop_images
  • crop_masks
  • crop_data
  • boundary_mask
size1.00

The single most repeated trick in inpainting-adjacent ComfyUI workflows is: don't sample the whole image for a small fix, crop to just the region that matters, sample that at full resolution, then paste it back exactly where it came from. It's why FaceDetailer-style tools exist, and it's the whole idea behind the popular Inpaint Crop and Stitch node that's become close to standard practice. This node is ComfyUI-WJNodes' own implementation of that exact pattern, built specifically around square, mask-centered crops.

It's part of a personal utility pack from an author who publishes under the handle 807502278 rather than a name - small, unglamorous, and built to solve the author's own problems, which happen to be common ones.

The two things it does

This node does two jobs depending on what you feed it, and it's worth being clear about which mode you're in:

Crop mode (the default, when crop_data is left empty): give it images and a mask, and it finds the mask's bounding region, crops a square around its center, and hands back the cropped image, cropped mask, and - critically - a crop_data object that remembers exactly where that crop came from. If your mask boundary would run past the edge of the source image, the Exceeding behavior (configured via an option node, or the crop's own defaults) fills the overflow rather than erroring.

Paste-back mode (when crop_data is supplied): feed it a processed image plus the crop_data you saved from the crop step, and it stretches that image back to the original crop size and hands you the full result pasted into place. In this mode, the cropping parameters below are ignored - the crop_data carries everything needed.

A batch note worth knowing up front, straight from the pack's own docs: one mask against many images crops all of them the same way; one image against many masks produces one crop per mask; and if both sides have more than one item but don't match in count, the extra ones are silently dropped rather than erroring.

Inputs and outputs that matter

  • images - required. If you don't supply a mask, the node falls back to the image's own alpha channel for the crop boundary.
  • size - a scale factor around the mask's own bounding box (default 1, range roughly 0.01–300). Leave it at 1 to crop tight to the mask; push it up if you want more surrounding context in the crop instead of just the masked region itself.
  • masks (optional) - the mask defining the crop boundary. Without one, the image's alpha channel is used instead.
  • crop_data (optional) - supply this to switch the node into paste-back mode.
  • option (optional) - a crop_option object from mask_crop_option_Basic or mask_crop_option_SmoothCrop, carrying threshold, fill-color, and smoothing settings.

Four outputs: crop_images and crop_masks (the cropped result), crop_data (save this for the paste-back step later), and boundary_mask.

Installing it

ComfyUI Manager: search "ComfyUI-WJNodes." Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/807502278/ComfyUI-WJNodes.git

Restart ComfyUI. No models or heavy dependencies - this is pure image/mask math.

Where people trip up

The most common mistake is forgetting this is a two-step node wearing one name. If you're only using crop mode and never routing a crop_data back through the same node later, you've cropped an image and thrown away the ability to put it back - which is fine if that's genuinely all you need, but usually the whole point of this pattern is the round trip.

The second is the silent batch-count drop mentioned above: if your image and mask batches end up different sizes somewhere upstream, you won't get an error, you'll just quietly lose the extras. Worth double-checking batch counts if your output has fewer results than you expected. And because this pack has no community troubleshooting trail anywhere, when something behaves unexpectedly, the README's own function-by-function notes (bundled straight into the node's tooltip) are worth reading closely before assuming it's a bug.

CategoryWJNode/ImageEdit/mask_crop

Inputs (5)

NameTypeDefaultDescription
imagesIMAGE
sizeFLOAT1.000.01–300
masksoptMASK
crop_dataoptcrop_data
optionoptcrop_option

Outputs (4)

NameTypeDescription
crop_imagesIMAGE
crop_masksMASK
crop_datacrop_data
boundary_maskMASK