Expand Bounding Box by Ratio
Loosen the Box Before You Crop, by a Ratio of Its Own Size
- bbox
- BBOX
Detection boxes are famously skull-tight. A face detector gives you exactly the face, no hair, no neck, and cropping that box gives you a portrait that feels like a mugshot. ExpandBBoxByRatio is the fix that turns a tight box into a comfortable one: it grows each edge by a ratio of the box's own width or height.
It's the node you slip between "found the region" and "crop it" - the difference between BBoxCrop spitting out a forehead-to-chin sliver and giving you a usable crop with breathing room. Because expansion is proportional to the box itself, it works the same whether your detection found a 20-pixel face or a 400-pixel one.
How it works
Each of the four edges has its own ratio input: top_ratio, bottom_ratio, left_ratio, right_ratio (defaults all 0.1). The math is simple - a side grows by box height × ratio for top/bottom, box width × ratio for left/right - and each side is independent, so you can widen without raising the ceiling.
The ratios go negative too (down to −0.5), which shrinks the box instead of growing it. That's occasionally useful - tightening a loose detection, or trimming padding that a detector added - but mostly you'll be pushing ratios up, not down.
Inputs and outputs
- bbox - the box to expand (the pack's
BBOX). - top_ratio / bottom_ratio / left_ratio / right_ratio - per-side expansion as a fraction of the box's own height/width. 0.1 default; up to 100; negative shrinks.
Output is the expanded BBOX, carrying the original label through.
Where it fits
The standard chain is MaskToBBox (or any detector) → ExpandBBoxByRatio → BBoxCrop. Want a roomier face crop? Set top/bottom to 0.15 and left/right to 0.1. Building an inpaint mask that needs to cover more than the detected object? Expand, then DrawBBoxMask. It's also the polite way to make sure a crop actually has context, so a downstream detailer or upscaler has something to work with.
Installing it
It's part of Duanyll Nodepack:
cd ComfyUI/custom_nodes
git clone https://github.com/Duanyll/duanyll_nodepack
or via ComfyUI Manager. No models - it's arithmetic on a rectangle.
Gotchas
The ratio is relative to the box, not the image, so a 0.1 default on a tiny box is a tiny expansion - on a huge box it's a big one. That's the design, and it's usually what you want, but don't expect a "10% of the image" reading. Also, nothing here clips to the canvas: expanding a box near the image edge will happily push it off-canvas, and downstream nodes (BBoxCrop, DrawBBox) clamp or error on that. If you're expanding aggressively, expect to manage the edges yourself.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| bbox | BBOX | — | |
| top_ratio | FLOAT | 0.10-0.5–100 | — |
| bottom_ratio | FLOAT | 0.10-0.5–100 | — |
| left_ratio | FLOAT | 0.10-0.5–100 | — |
| right_ratio | FLOAT | 0.10-0.5–100 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| BBOX | BBOX | — |