Nodes/ComfyUI-TinyBee/Crop/Grow Image to Bounds
ComfyUI Node

Crop/Grow Image to Bounds

The crop node that doesn't panic when the box hangs off the image

By TinyBeeman·Created about a year ago·Updated 10 days ago· 1
Crop/Grow Image to Bounds
  • image
  • tinyrect
  • imgOriginal
  • rectOriginalCrop
  • image
  • mask
  • padded
  • outpaintLeft
  • outpaintRight
  • outpaintTop
  • outpaintBottom
x0
y0
width512
height512
allowGrowthfalse
outpaintColor(255, 255, 255)

Every crop node does the easy part: cut out the box. The hard part is what happens when the box doesn't fit - a detector that overshoots the edge, a rect you reshaped toward the boundary. Most crop nodes just clip and silently give you less than you asked for. Crop/Grow Image to Bounds is the one that tells you exactly what it had to do, and can grow the canvas to make the crop whole again.

It takes an image and an x, y, width, height box (or a TINYRECT for the same job) and returns the crop. By default it keeps only the part of the box that overlaps the source - no padding - and then reports, in four separate integers, how far the box extended past each edge (outpaintLeft, outpaintRight, outpaintTop, outpaintBottom). That reporting is the clever part: a downstream node can use those numbers to decide what to do, because you now know the crop is missing a 40-pixel strip on the right instead of guessing.

Growing the crop back out

Flip allowGrowth on and the behavior changes: instead of clipping, the node grows the output to the full requested size, filling the area beyond the source with outpaintColor (a "(255, 255, 255)" or "#ffffff" style string, default white). And it hands you a mask marking which pixels are synthetic fill (1.0) versus real image content (0.0) - exactly what you need if you plan to inpaint that fill region back into something plausible, or tell the next step which parts are trustworthy.

The padded output is a quick boolean for "did any growth happen at all," which makes for clean routing: check padded, and only run the fill-inpainting pass when it's true.

The secret third mode: recovering real pixels

There's an optional pair worth knowing about - imgOriginal and rectOriginalCrop. If your current image was itself cropped out of a larger original using rectOriginalCrop, connecting both makes the node reach back into the original image for pixels beyond the current crop's bounds, using real content before it ever falls back to outpaintColor. It's the difference between "fill the missing strip with white" and "just re-crop the original, which actually had that strip." For detailing loops that crop, refine, and composite back, this removes a whole class of seam artifacts.

Install and gotchas

Standard TinyBee install:

cd ComfyUI/custom_nodes
git clone https://github.com/TinyBeeman/ComfyUI-TinyBee

or ComfyUI Manager → "ComfyUI-TinyBee" → restart. No models. This node does need pillow (in the pack's requirements.txt with jsonata; Manager installs them). The default allowGrowth is false, so out-of-bounds boxes come back clipped by default - the outpaint ints will tell you it happened, but if you expected a full-size crop, that's your first suspect. It sits in 🐝TinyBee/Images, ready to receive a tinyrect from the pack's detector-parsing and rect-family nodes.

Category🐝TinyBee/Images

Inputs (10)

NameTypeDefaultDescription
imageIMAGE
xINT0-100000–100000
yINT0-100000–100000
widthINT5121–100000
heightINT5121–100000
allowGrowthBOOLEANfalse
outpaintColorSTRING(255, 255, 255)
tinyrectoptTINYRECT
imgOriginaloptIMAGE
rectOriginalCropoptTINYRECT

Outputs (7)

NameTypeDescription
imageIMAGE
maskMASK
paddedBOOLEAN
outpaintLeftINT
outpaintRightINT
outpaintTopINT
outpaintBottomINT