ComfyUI Node

Smart Mask Crop

Smart Mask Crop — the focused-region editor's front end

By amortegui84·Created 5 months ago·Updated 3 months ago· 3
Smart Mask Crop
  • image
  • mask
  • stitcher
  • cropped_image
  • cropped_mask
  • cropped_mask_image
  • preview_image
  • info
context_expand1.15
use_region_guidancetrue
mask_expand_percent0.0
mask_feather_percent0.0
resize_modeupscale_to_target_if_smaller
target_width1024
target_height1024
downscale_algorithmbilinear
upscale_algorithmbicubic
device_modegpu (much faster)
depad_florencetrue
use_region_mask_defaultstrue
edit_size_scale_percent100
region_info
target_size_moderegion_recommended
target_aspect_ratioregion_info

Some editors - GPT Image being the big one - accept a real mask. That changes the game versus the Nano Banana path, because you can crop a tight region around what you're editing, send the model only that crop plus a matching mask, and never let it touch the rest of the frame. Smart Mask Crop is the node that prepares that crop. It's the most configurable node in this pack, and it's the front end for the whole "local masked edit" path:

Florence-2 or SAM selector -> mask -> Smart Mask Crop
    -> cropped_image      -> GPT Image Edit (image_1)
    -> cropped_mask_image -> GPT Image Edit (mask_image)
    -> Smart Mask Stitch -> final image

The output wiring is the thing to get right: cropped_mask_image goes straight into the editor's mask_image (not the original Florence mask - it won't match the crop), and info can drive the editor's sizing so the requested output size stays aligned with the crop.

The inputs that matter

Required: image, mask, and then these three do most of the work:

  • context_expand (1.0–4.0, default 1.15) - grows the detected region before cropping, so the model sees a little original context around the object. Faces like 1.1–1.2; the README's guidance is 1.1–1.2 for faces, more or less as-is for other regions.
  • target_size_mode - the sizing policy. region_recommended (default) trusts the Florence region defaults; manual_width_height uses target_width/target_height; max_for_aspect_ratio picks the largest valid GPT Image size for a given target_aspect_ratio. For a portrait try-on where the source is already the right shape, the README's tip is max_for_aspect_ratio with target_aspect_ratio = input_image - that stops a glasses region hint from forcing a horizontal 16:9 crop on a 4:5 workflow.
  • mask_expand_percent / mask_feather_percent - the edit mask's expansion and softness. Both default to 0, which means "use the region's defaults" while use_region_mask_defaults is on; disable that toggle if you want a hard mask you fully control.

Worth knowing: edit_size_scale_percent (25–200, default 100) scales the edit output as a percentage of the target - 50 = half size, useful when you want a quick cheap pass before committing to full detail.

Outputs: stitcher (into Smart Mask Stitch), cropped_image + cropped_mask_image (into the editor), plus cropped_mask, preview_image, and info (valid JSON with the final target size - feed it to GPT Image's region_info via auto_from_region).

Keep it local

One real mistake people make with GPT Image in this flow: prompting it to "recompose the whole portrait" or "fix the lighting globally." The editor will happily do that to the crop, which is a tiny region of the whole image - and then the stitch pastes an island of changed lighting back. The README is explicit: describe only the object, name the reference image, and explicitly preserve pixels outside the mask. The global fixes belong in a full-frame pass, not the local masked edit.

Install

Search comfyui-inpaint-cropstitch-nb2 in ComfyUI Manager or:

cd ComfyUI/custom_nodes
git clone https://github.com/amortegui84/comfyui-inpaint-cropstitch-nb2

Restart. This crop node runs locally - the FAL key is needed on the selector (if you use this pack's FAL one) and the editor, not here. And if depad_florence sounds familiar, it's the same rule as the rest of the pack: keep it True for Florence masks, False for SAM or hand-drawn ones that are already at exact resolution.

Categoryinpaint/masked

Inputs (18)

NameTypeDefaultDescription
imageIMAGE
maskMASK
context_expandFLOAT1.151–4Grow the detected mask region before cropping.
use_region_guidanceBOOLEANtrueUse Florence region metadata to override context and target size when available.
mask_expand_percentFLOAT0.00–100Extra expansion applied to the edit mask after crop. 0 uses region defaults when guidance is enabled.
mask_feather_percentFLOAT0.00–100Softens the edit mask edges after crop. 0 uses region defaults when guidance is enabled.
resize_modeCOMBOupscale_to_target_if_smaller3 options: keep_local_size, upscale_to_target_if_smaller, resize_to_target
target_widthINT102464–16384
target_heightINT102464–16384
downscale_algorithmCOMBObilinear6 options: nearest, bilinear, bicubic, lanczos, box, hamming
upscale_algorithmCOMBObicubic6 options: nearest, bilinear, bicubic, lanczos, box, hamming
device_modeCOMBOgpu (much faster)2 options: gpu (much faster), cpu (compatible)
depad_florenceBOOLEANtrueRemove Florence2's internal square letterbox padding before resizing the mask. Keep True when the mask comes from Florence2Run (kijai). Disable only if your mask is already at the exact source image resolution.
use_region_mask_defaultsBOOLEANtrueWhen enabled, 0 mask expand/feather values use Florence region defaults. Disable it when you need a hard mask.
edit_size_scale_percentFLOAT10025–200Scale the edit output resolution as a percentage of the target. 100 = full region recommended size (e.g. 2752x1536 for glasses). 50 = half size, 150 = 150%%. Preserves aspect ratio. The scaled size flows via info to GPT Image nodes using auto_from_region, so both crop and output stay aligned.
region_infooptSTRING
target_size_modeoptCOMBOregion_recommendedregion_recommended uses Florence/region defaults; manual_width_height uses target_width/target_height; max_for_aspect_ratio uses the largest valid GPT Image size for target_aspect_ratio.
target_aspect_ratiooptCOMBOregion_infoAspect ratio used when target_size_mode is max_for_aspect_ratio.

Outputs (6)

NameTypeDescription
stitcherSTITCHER
cropped_imageIMAGE
cropped_maskMASK
cropped_mask_imageIMAGE
preview_imageIMAGE
infoSTRING