Nodes/Comfyui_LG_Tools/AFL Target box cropV2
ComfyUI Node

AFL Target box cropV2

Crop exactly the region your mask found, sized for real inpainting

By LAOGOU-666·Created 2 years ago·Updated 7 months ago· 369
AFL Target box cropV2
  • image
  • mask
  • cropped_image
  • crop_box
  • cropped_mask
resize_modelanczos
Box_grow_factor1.00
megapixels1.0
divisible_by8
ratioauto
startup_threshold0.40
fill_color#FFFFFF

AFL2:MaskBoxCropNodeV2 ("AFL Target box cropV2") is the crop half of the pack's crop → inpaint → paste-back loop. Feed it an image and a mask, and it crops the image down to the bounding box of whatever the mask covers, then resizes that crop to a size that's actually good for inpainting - with controls for aspect ratio, target megapixels, and resolution divisibility. It outputs the cropped image, the crop coordinates, and the cropped mask, all in sync.

This is a genuinely useful node, and it's the one the KB's inpainting doc is describing when it explains why masked inpainting still beats edit models: "Crop a small region, render it at the model's native resolution, paste it back. A 64px eye gets 1024px of generation budget." That's exactly this node's job - take the tiny region you care about and give it the full resolution budget of your sampler. Pair it with the pack's AFL2:ImageRestoreNodeV2, which pastes the inpainted crop back onto the original, and you've got the classic Impact-Pack-style detailer loop without needing the impact pack.

The inputs that matter

  • image (IMAGE) and mask (MASK) - the source and its selection. The mask defines the region; the crop box is computed from its bounding box.
  • resize_mode (enum) - interpolation for the resize: lanczos is the quality default, then bilinear, bicubic, nearest-exact for speed or pixel-art. (The schema also lists a NaN option that you can safely ignore.)
  • Box_grow_factor (FLOAT, 1–5, default 1) - pad the crop region around the mask. 1.0 means no padding; bump it to give the inpainter context room, which usually helps it blend.
  • megapixels (FLOAT, 0.1–10, default 1) - target size of the output crop, where 1 MP = 1024×1024. This is your "give the eye a 1024px budget" dial.
  • divisible_by (INT, default 8) - force the output resolution to be divisible by this. Leave 8; some samplers and attention blocks are picky.
  • ratio (enum) - force an aspect: auto (detect the mask's nearest match from 1:1, 4:3, 3:4, 16:9, 9:16) or pick one explicitly.
  • startup_threshold (FLOAT, default 0.4) - the smart-skip: if the mask box already covers this fraction of the image, skip the ratio/grow re-shaping and crop near-as-is. Handy when the subject nearly fills the frame.
  • fill_color (STRING, default #FFFFFF) - color for any border padding when the reshaped crop extends past the image edge. Hex or color name.

What comes out

Three outputs: cropped_image, crop_box (CROPBOX - the coordinates you feed to the restore node), and cropped_mask (mask resized to match the crop, for conditioning the inpaint). Keeping the mask in sync with the crop is what makes the restore step possible.

Installing it

Part of Comfyui_LG_Tools:

cd ComfyUI/custom_nodes
git clone https://github.com/LAOGOU-666/Comfyui_LG_Tools.git
pip install -r requirements.txt

Or via ComfyUI Manager → "Comfyui_LG_Tools" → restart. It's under Add Node → AFL → Mask.

Common issues

  • "The crop is huge for a tiny mask." megapixels and Box_grow_factor are working as designed - a small region gets upscaled to the target MP so the inpaint has resolution. Lower megapixels if it's oversized.
  • "The crop goes off the edge." That's what fill_color exists for - the reshaped box is larger than the image, and the padded region gets filled. Pick a color that blends, or keep the grow factor modest.
  • "Crop doesn't match my mask." The mask output tracks the crop, not the other way around - if you resized the mask upstream, the box follows the mask you gave it.

This is a well-thought-out node and the strongest "serious" tool in the pack outside the background remover. Learn it together with ImageRestoreNodeV2 and you'll wonder how you inpainted before.

CategoryAFL/Mask

Inputs (9)

NameTypeDefaultDescription
imageIMAGE
maskMASK
resize_modeCOMBOlanczos5 options: NaN, lanczos, nearest-exact, bilinear, bicubic
Box_grow_factoroptFLOAT1.001–5裁剪区域的扩展倍数,1.0表示不扩展,大于1.0表示按比例扩大
megapixelsoptFLOAT1.00.1–10目标图像的百万像素数,以1024*1024为1百万像素基准
divisible_byoptINT81–1024目标分辨率必须被此数字整除
ratiooptCOMBOauto裁剪比例模式,auto为自动检测最接近比例
startup_thresholdoptFLOAT0.400–1当mask的box面积与输入图像的面积占比达到此阈值时,跳过ratio和box_grow_factor判断
fill_coloroptSTRING#FFFFFF边界超出时的填充颜色,支持hex格式(#FFFFFF/#FFF)或颜色名称(red/blue/green等)

Outputs (3)

NameTypeDescription
cropped_imageIMAGE
crop_boxCROPBOX
cropped_maskMASK