Nodes/Image Crop By Mask And Resize (Multiple)/Image Crop By Mask And Resize (Multiple)
ComfyUI Node

Image Crop By Mask And Resize (Multiple)

When 'Multiple of 16' Isn't the Multiple You Need

By mo-akkakk·Created about a month ago·Updated about a month ago· 1
Image Crop By Mask And Resize (Multiple)
  • image
  • mask
  • images
  • masks
  • bbox
base_resolution512
padding0
min_crop_resolution128
max_crop_resolution512
multiple16

Every detect-and-crop workflow eventually hits the same wall. You've detected a face, a hand, or an object, you've got a nice tight mask around it, and now you need to hand that region to a model that is very picky about resolution. ComfyUI's latent space works in multiples of 8, most pipelines want multiples of 16, and Flux wants multiples of 64. The classic KJNodes "Image Crop By Mask And Resize" node handles the cropping - but it's hardcoded to round to 16, and you can't change that without editing the source. This node is the small fork that fixes exactly that one thing: it lets you pick the step size yourself.

What it actually is

A standalone fork of kijai's KJNodes node, with one added input and nothing else changed. Feed it an image and a mask, it crops each batch item to the mask's bounding box and resizes everything to a uniform resolution. The multiple input replaces the hardcoded "round to 16" with a value you choose - 8, 16, 32, 64, whatever your downstream model demands. If you already know the KJNodes node, you know this one; the only thing to learn is the new knob.

It's worth saying what it's not: it doesn't composite anything back, and it doesn't cut your object out onto a transparent background. It crops and resizes, period. People who want the extracted object pasted back onto the original (or exported with transparency) reach for something like ImageCompositeMasked after this - the crop is just the middle step.

How it works

The mechanism is easy to follow in the source. The mask is rounded to binary, the node finds the bounding box of nonzero pixels, applies your padding (clamped so the crop never runs off the image edge), then clamps the box to min_crop_resolution / max_crop_resolution. Across the batch it takes the widest box, rounds its width and height up to a multiple of multiple, and uses that as one uniform crop size centered on each item's own box. Everything is then resized to a common target derived from base_resolution and the batch's widest aspect ratio - lanczos for the image, bilinear for the mask, which keeps mask edges from ringing.

Because all crops share one size, you can stack the results and feed them to a model without shape mismatch. That's the whole point.

The inputs that matter

Only a few of the seven inputs deserve your attention as a beginner:

  • multiple - the whole reason this node exists. Set it to the alignment your model needs (16 for most things, 64 for Flux, 8 if you're feeding a VAE directly).
  • base_resolution - the target for the longer side after resizing. 512 is the default and is a sensible starting point.
  • padding - extra pixels around the mask box so the model has context to work with. Zero by default; a little goes a long way for detailers.
  • min_crop_resolution / max_crop_resolution - clamps on the raw box before resizing. Defaults are 128 and 512; if your mask's box is bigger than the max, the crop gets squeezed.

The outputs are images, masks, and bbox - the bbox is the (x0, y0, x1, y1) region each crop came from, handy if you're wiring the crops back into a compositing or detection loop.

Installing it

No model downloads, no heavyweight dependencies, no requirements.txt at all - the code only touches torch and ComfyUI's built-in common_upscale, so install is painless:

cd ComfyUI/custom_nodes
git clone https://github.com/mo-akkakk/comfyui-image-crop-by-mask-multiple.git

Restart ComfyUI and you're done. Or use ComfyUI Manager and search for "Image Crop By Mask And Resize (Multiple)". One gotcha: it registers under the KJNodes/image category, so it shows up in the KJNodes menu in your node list - but it's a standalone pack, so you don't need KJNodes installed to use it.

Where people get burned

The author's own README is honest about the main trade-off: width and height are rounded to multiple independently, so on some aspect ratios the resize introduces slight stretching. It keeps output size predictable and bounded - you'll never get a runaway resolution - but a portrait crop that lands at 448x640 instead of 448x630 is the price. The fix is padding, which changes the box's aspect ratio before the rounding happens.

Also watch the empty-mask case: with no nonzero pixels, the bbox collapses to zero and the node falls back to centering. And remember this is a young fork with essentially no community footprint yet - the defaults are sensible, but if you hit something odd, the repo is small enough that the source is your documentation. For a one-trick node, that's not a bad place to be.

CategoryKJNodes/image

Inputs (7)

NameTypeDefaultDescription
imageIMAGE
maskMASK
base_resolutionINT5120–16384
paddingINT00–16384
min_crop_resolutionINT1280–16384
max_crop_resolutionINT5120–16384
multipleINT161–16384Forces output image/mask width & height (and bbox size) to be an exact multiple of this value.

Outputs (3)

NameTypeDescription
imagesIMAGE
masksMASK
bboxBBOX