Nodes/ComfyUI-BrainDead/BD Crop and Center
ComfyUI Node

BD Crop and Center

Crop an image to the bounding box of its mask (like AILab Crop-to-Object), with optional centering. Bbox comes from the wired mask, else the image's alpha (RGBA) or its difference from the border background colour. canvas_width = canvas_height = 0 → output the TIGHT CROP (just the bbox region). canvas_width / canvas_height > 0 → place the crop on that canvas at the anchor (center / edges / corners), with optional scale_to_fit — this is the centering option. flatten_to_mask makes content show only inside the mask (background fills the rest of the crop). Outputs the cropped image, its mask, and a transform JSON.

By BizaNator·Created 7 months ago·Updated 16 days ago· 14
BD Crop and Center
  • image
  • mask
  • image
  • mask
  • transform
anchorcenter
canvas_width0
canvas_height0
pad0
threshold0.02
scale_to_fitfalse
background_hex#000000
flatten_to_masktrue
Category🧠BrainDead/Segmentation

Inputs (10)

NameTypeDefaultDescription
imageIMAGE
maskoptMASKMask defining the crop region (its bounding box). If absent, the bbox is taken from the image's alpha (RGBA) or its difference from the border bg.
anchoroptCOMBOcenterPlacement on the canvas (only used when canvas_width/height > 0). center = shared pivot; top/bottom/left/right + corners anchor by that edge.
canvas_widthoptINT00–81920 = output the TIGHT CROP (no canvas). >0 = place the crop on a canvas this wide at the anchor. (If only one of width/height is >0, the other uses the input size.)
canvas_heightoptINT00–81920 = tight crop (no canvas). >0 = canvas height for centering.
padoptINT00–1024Pixels of margin added around the mask bbox before cropping (AILab-style padding).
thresholdoptFLOAT0.020–1Mask/alpha value above which a pixel counts as content (for the bbox).
scale_to_fitoptBOOLEANfalseCanvas mode only. On = scale the crop to FIT the canvas (longest side → canvas), preserving aspect (up or down), then anchor. Off = keep original pixel scale (re-pivot only).
background_hexoptSTRING#000000Background fill for masked-out / padded regions.
flatten_to_maskoptBOOLEANtrueOn = content shows only inside the mask; the background colour fills the rest of the crop (and alpha = mask for RGBA). Off = output the raw rectangular crop (exactly like AILab Crop-to-Object).

Outputs (3)

NameTypeDescription
imageIMAGE
maskMASK
transformSTRING