ComfyUI Node

CropImageByBbox

CropImageByBbox – ComfyUI Node

By lldacing·Created 3 years ago·Updated 8 months ago· 96
CropImageByBbox
  • image
  • bbox
  • crop_image
  • mask
  • crop_bbox
  • w
  • h
margin16

What it is

CropImageByBbox does the one thing every detect-then-refine workflow needs at some point: take a bounding box and actually cut that region out of the image. It's the "crop" half of the crop-refine-paste-back pattern you'll recognize from Impact Pack's FaceDetailer and basically every face-swap or region-detailer workflow - detect a region, crop it out, run a focused sampling pass on just that piece at proper resolution, then composite it back into the original frame. A face occupying a tiny corner of a 1024×1024 image gets ignored by a normal sampling pass; crop it out first and it gets the model's full attention.

This node's bbox format is fixed: corner coordinates, [x, y, x1, y1] - top-left and bottom-right, not width/height. If your bbox is coming from something that outputs (x, y, w, h) instead, you'll need a format conversion first (this same pack ships BboxToBbox for exactly that, though it's not covered here).

How it works

Give it an image and a bbox, and it crops out that rectangle - plus a configurable margin of extra padding around the edges, so you're not cropping exactly to the detected box with zero breathing room (useful because detectors are rarely pixel-perfect, and a tight crop can clip part of the thing you actually wanted). Along with the cropped image it hands back a matching mask, the actual bbox used for the crop (which may differ slightly from what you fed in once the margin and image boundaries are accounted for), and the resulting width and height as plain integers - handy for anything downstream that needs to know the crop's dimensions without measuring the image itself.

Inputs and outputs

  • image (IMAGE, required) - the source image.
  • bbox (BBOX, required) - corner-format [x, y, x1, y1].
  • margin (INT, default 16) - extra pixels of padding added around the box before cropping.

Outputs: crop_image (IMAGE), mask (MASK), crop_bbox (BBOX - the box actually used, post-margin), w (INT), h (INT).

Installing it

Ships inside comfyui-easyapi-nodes. Through ComfyUI Manager, search "comfyui-easyapi-nodes" and install. Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/lldacing/comfyui-easyapi-nodes.git
cd comfyui-easyapi-nodes
pip install -r requirements.txt

Restart ComfyUI, and git pull in the same folder later to upgrade. This node is plain image-array math with no heavyweight dependency of its own, but run the requirements install anyway - several sibling nodes in the pack (the InsightFace one especially) do need extra packages, and skipping this step is the most common reason a fresh install of the pack half-fails on load.

Common issues

"My crop is offset or the wrong size." First suspect is bbox format. This node wants [x, y, x1, y1] corners specifically - if your detector emits (x, y, w, h) and you feed it in raw, the crop will land in the wrong place because the third and fourth numbers get interpreted as a second corner instead of a width and height.

"The crop clips part of what I wanted (a chin, an ear, an edge of an object)." Bump margin up. Detectors tend to draw tight boxes, and a small pixel budget around the edges is usually the difference between a clean crop and one that cuts something off. This is also the standard fix used across the whole detect-crop-refine family of workflows, not just this node.

"How do I get the cropped result back into the original image after processing it?" This node only does the crop half. You'll need a paste-back / composite step afterward - the standard move is ImageCompositeMasked using the mask output this node gives you, so only the processed region gets written back and everything outside the crop stays byte-for-byte untouched.

"crop_bbox came back different from the bbox I put in." That's expected, not a bug - it reflects the margin you added and any clamping against the image's actual edges (a box near the border can't grow past the frame). Use crop_bbox, not your original bbox, if you need the exact region that was cropped for a later paste-back.

CategoryEasyApi/Bbox

Inputs (3)

NameTypeDefaultDescription
imageIMAGE
bboxBBOX
marginINT16bbox矩形区域向外扩张的像素距离

Outputs (5)

NameTypeDescription
crop_imageIMAGE
maskMASK
crop_bboxBBOX
wINT
hINT