Image Crop By Mask And Resize (UTK)
Crop by mask, then get a clean, AI-friendly resolution out the other side
- image
- mask
- images
- masks
- bbox
The classic detect-crop-refine loop needs one thing to go right every time: a tight, correctly-sized crop around your subject. Impact Pack built the whole FaceDetailer industry on it. Image Crop By Mask And Resize (UTK) is the crop half of that job - take a mask, find the region, crop to it, and land on a resolution that's actually usable downstream instead of some oddball number. It's a port of the KJNodes approach, and it solves the annoying math so you don't have to.
How it works
Give it an image and a mask. It finds the masked region, computes a crop box, crops the image (and the mask, so both stay aligned), and resizes the result to a sane target size. The three numbers that control everything:
- base_resolution (default 512) - the target length for the crop's longer side. This is the "how big do I want the final crop" dial.
- min_crop_resolution / max_crop_resolution (defaults 128 / 512) - guardrails. If the detected region is tiny or enormous, it clamps within these bounds instead of cropping something useless.
- padding (default 0) - extra margin around the detected region. Bump it when your subject touches the box edge and gets chopped.
It also rounds dimensions to multiples of 16, which is the AI-friendly alignment every sampler and latent space quietly expects. Little thing, saves you from the "latent size mismatch" class of errors.
Resize methods
resize_method picks how the crop lands on base_resolution:
fill(default) - scales to completely fill the target, cropping edges that overflow. Best for subject-centric crops.crop- scales to fit within the target and pads with black to hit the size.letterbox- fit within the target, letterbox bars to keep aspect ratio.stretch- force the target dimensions, distorting if the aspect ratio differs. Usually the wrong answer; it's there for compatibility.
upscale_method (default lanczos) picks the interpolation - nearest/bilinear/bicubic/lanczos. Lanczos is the quality default and fine for this job.
Outputs
Three: images (the cropped batch), masks (the aligned mask, so you can feed both into an inpainting pass in perfect registration), and bbox (the crop coordinates - handy if you need to paste the result back where it came from).
Installing it
Standard pack install - ComfyUI Manager → search ComfyUI-UniversalToolkit, or:
cd ComfyUI/custom_nodes
git clone https://github.com/whmc76/ComfyUI-UniversalToolkit
cd ComfyUI-UniversalToolkit && pip install -r requirements.txt
Then restart. Needs numpy/opencv/Pillow, all in the pack's requirements.
Gotchas
- It processes batches as a unit - all images get the same unified crop dimensions, so a batch of different-sized subjects won't produce ragged outputs. Good, but it means one outlier region drags the whole batch's size.
- Empty masks produce the full image - a mask with nothing in it falls back to cropping the whole frame rather than erroring. That's forgiving, but it's also why a workflow "works" while silently not cropping. Check your mask validity upstream (a Check Mask node pairs nicely).
cropandletterboxpad with black - if you're cropping something with a transparent background, that black padding will show up in your inpainting. Preferfillunless you need the exact target size.
If your detect-crop-refine chain keeps choking on awkward resolutions, this is the node that makes the whole loop mechanical. It's not flashy, but it's exactly the kind of utility that stops a workflow from being fiddly.
Inputs (8)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| mask | MASK | — | |
| base_resolution | INT | 51264–16384 | — |
| padding | INT | 00–16384 | — |
| min_crop_resolution | INT | 12864–16384 | — |
| max_crop_resolution | INT | 51264–16384 | — |
| resize_method | COMBO | fill | Method for resizing to target resolution |
| upscale_method | COMBO | lanczos | Interpolation method for upscaling |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| images | IMAGE | — |
| masks | MASK | — |
| bbox | BBOX | — |