Nodes/ComfyUI_Swwan/Bounded Image Crop (Swwan)
ComfyUI Node

Bounded Image Crop (Swwan)

Crop with a bounding box, not a blind guess

By aining2022·Created 9 months ago·Updated 10 days ago· 33
Bounded Image Crop (Swwan)
  • image
  • image_bounds
  • IMAGE

Some crops are a composition decision and some are a measurement. If a mask-detection node (or a bounding-box generator, or a face detector) already told you where the interesting region is, you don't want to eyeball crop coordinates - you want to hand that box to a crop node and get the pixels inside it. BoundedImageCrop is that node. It takes an image plus an IMAGE_BOUNDS value and cuts out exactly the region described.

It's a port of the WAS Node Suite's Bounded Image Crop, and it exists in this pack for the same reason most of ComfyUI_Swwan exists: the author uses these nodes daily and bundled the ones worth keeping. The IMAGE_BOUNDS type is a box defined as (rmin, rmax, cmin, cmax) - row min/max, column min/max. You get one from WAS's mask-to-bounds nodes, or from the pack's own BoundedImageCropWithMask (which emits the bounds it computed).

How it works

Two inputs, one output: image and image_bounds in, cropped IMAGE out. The crop is a straight tensor slice - image[i][rmin:rmax+1, cmin:cmax+1, :] - so it's exact, fast, and won't resample or change colors. No interpolation, no aspect-ratio math, no surprises: the output is pixel-identical to the region you asked for.

The batch behavior is the one thing worth understanding. If the image batch and the bounds list are the same length, it crops each image with its matching bounds, one-to-one. If they don't match, it takes the first bounds and applies it to every image in the batch. That's a deliberate design choice - apply one detected region across a whole batch of frames - but it's also the easiest way to get a batch of crops you didn't intend if you forgot to sync counts.

Installing it

It's part of ComfyUI_Swwan. Install the pack via ComfyUI Manager (search "ComfyUI_Swwan"), or:

cd ComfyUI/custom_nodes
git clone https://github.com/aining2022/ComfyUI_Swwan
pip install -r ComfyUI_Swwan/requirements.txt

Where people get burned

The main trap is upstream: the node validates that rmin <= rmax and cmin <= cmax, and throws a clear error if a bounds object is malformed - but it does not check that the box is inside the image. An out-of-range bound gives you a mis-sized tensor or an error from torch, and the error message won't tell you it's a bounds problem. If you're feeding bounds computed from a differently-sized image, resize the bounds or the image first. And remember the one-to-many fallback: if your bounds count and image count disagree, it silently uses bounds[0] - check that's actually what you want before running a long batch.

CategorySwwan/image

Inputs (2)

NameTypeDefaultDescription
imageIMAGE
image_boundsIMAGE_BOUNDS

Outputs (1)

NameTypeDescription
IMAGEIMAGE