Nodes/comfyui-instantId-faceswap/Preprocess image for instantId
ComfyUI Node

Preprocess image for instantId

Crop, pad, resize, and build your control image in one go

By nosiu·Created 3 years ago·Updated about a year ago· 264
Preprocess image for instantId
  • image
  • mask
  • insightface
  • resized_image
  • mask
  • control_image
  • x
  • y
  • original_width
  • original_height
  • new_width
  • new_height
width1024
height1024
resize_mode
pad100

This is the node most InstantID faceswap workflows in this pack actually start from, once you've got a pose image and a mask drawn on it. It does three things at once: crops out the masked region (padded so there's context around the face), resizes that crop to dimensions InstantID can actually use, and - if you feed it an insightface app - builds the keypoint control image that ControlNetInstantIdApply needs downstream. One node, does most of the prep work.

How it works

You draw a mask over the face (and anything else you want to change - hair, a hat, whatever) on your pose image. This node finds the masked area, wraps it in a square, enlarges that square by pad pixels in every direction, then resizes the result down to multiples of 8 - SDXL's usual requirement. If you pass it an insightface input, it also runs face detection on that crop and generates a control_image: the rendered keypoint map InstantID's ControlNet actually conditions on. Skip the insightface input and this node still works fine as a general-purpose inpaint crop-and-resize utility, no InstantID-specific behavior required.

The inputs and outputs that matter

  • image / mask - your pose photo and the region you want to change. The mask has to actually contain the face for this to make sense.
  • width / height (both default 1024) - target size, interpreted according to resize_mode.
  • resize_mode - four options: auto (calculates size so the area matches width×height - the README's recommendation for SDXL is width: 1024, height: 1024 with this mode), free (uses your exact width/height), scale by width, scale by height (derives the other dimension from aspect ratio).
  • pad (default 100) - how far to expand the mask's bounding square in each direction before cropping. Too tight and insightface can't find enough face context; too loose and you're wasting resolution on background.
  • insightface (optional) - feed this to get a control_image out; omit it to use this purely as an inpaint-crop tool.

Outputs beyond the obvious resized_image, mask, and control_image: x, y, original_width, original_height, new_width, new_height - the geometry you need to composite your result back into the original full-size image afterward.

Install

ComfyUI Manager: search comfyui-instantId-faceswap. Manual: git clone https://github.com/nosiu/comfyui-instantId-faceswap into custom_nodes/, pip install -r requirements.txt. If you're using the insightface input, you need antelopev2 downloaded into models/insightface/models/antelopev2 first - this node calls into the same detection pipeline as LoadInsightface feeds elsewhere.

There's also an "Advanced" sibling node in this pack (not covered here) with per-edge padding and a choice of upscale method, if this node's single pad value doesn't give you enough control.

Common issues & troubleshooting

"No face detected in pose image." This is the pack's own most-cited error, and it happens right here when the insightface input can't find a usable face in your masked, padded crop. The README's own fixes, in order of how often people reach for them: draw a bigger mask, bump pad up so more context is included, or - if the face is at an extreme angle insightface just can't handle - skip detection entirely and draw your own keypoints with KpsDraw instead.

Control image looks wrong or the face is at a weird angle. The README flags this directly: at extreme rotation angles, the control_image this node builds can come out drawn incorrectly, because insightface's landmark detection gets unreliable past a certain tilt. AngleFromFace exists specifically to catch and correct for this before it becomes a problem.

Output resolution isn't what you expected. Remember auto mode targets area, not exact width/height - if you need precise pixel dimensions, use free instead.

CategoryInstantId Faceswap

Inputs (7)

NameTypeDefaultDescription
imageIMAGE
maskMASK
widthINT10240–16384
heightINT10240–16384
resize_modeCOMBO4 options: auto, free, scale by width, scale by height
padINT1000–16384
insightfaceoptINSIGHTFACE_APP

Outputs (9)

NameTypeDescription
resized_imageIMAGE
maskMASK
control_imageIMAGE
xINT
yINT
original_widthINT
original_heightINT
new_widthINT
new_heightINT