Nodes/comfyui-instantId-faceswap/PreprocessImageAdvanced
ComfyUI Node

PreprocessImageAdvanced

Crop, resize, and remember how to paste it back

By nosiu·Created 3 years ago·Updated about a year ago· 264
PreprocessImageAdvanced
  • image
  • mask
  • insightface
  • resized_image
  • mask
  • control_image
  • x
  • y
  • original_width
  • original_height
  • new_width
  • new_height
width1024
height1024
resize_mode
upscale_method
pad_top100
pad_right100
pad_bottom100
pad_left100

Face swapping at full image resolution wastes compute and hurts quality - SDXL doesn't need to wrestle with a 3000-pixel photo when the actual work is a few hundred pixels of face. This node does the unglamorous but load-bearing job of cutting that region out, padding it sensibly, and resizing it to something SDXL is actually good at, while handing you everything you need to stitch the result back into the original photo afterward.

What it is and why you'd reach for it

It's the advanced sibling of "Preprocess Image for InstantId" - same job, five extra dials, all of them about padding and resize quality. You'd reach for this one specifically when a single uniform pad value isn't enough: a face near the top of frame needs different headroom than one near the edge, and the basic node can't express that.

How it works

Feed it your pose image and a mask - hand-drawn, or built by MaskFromKps if you came from a keypoints-only workflow. It finds the mask's bounding box, wraps it in a square, and grows that square outward independently in each direction via pad_top, pad_right, pad_bottom, pad_left (each defaulting to 100 pixels), instead of the single pad value the basic version uses. The crop is then resized to your target dimensions - rounded down to a multiple of 8, since that's what SDXL's latent grid needs - using whichever upscale_method you pick: lanczos if quality matters, nearest-exact if you just need a fast test pass.

Plug in an insightface model as well (it's optional - leave it disconnected and this node is just a generic crop-and-resize-for-inpainting tool) and it also runs face detection on the crop to produce a control_image: the KPS-based control map that InstantID's ControlNet node needs to hold the face's geometry during generation. The pack's own docs are upfront about the failure mode here - at extreme rotation angles InsightFace's detection can misfire, and the resulting control_image comes out wrong. That's exactly what the pack's rotation nodes (Get Angle from Face, Rotate Image) exist to head off before you get here.

Inputs and outputs that matter

  • image / mask - your pose photo and the region to work on.
  • width / height (default 1024 each), paired with resize_mode - auto sizes the crop so its area matches width × height (the mode to use for SDXL, leaving width/height at 1024/1024), free uses your numbers exactly, and scale by width / scale by height locks the aspect ratio off one dimension.
  • pad_top / pad_right / pad_bottom / pad_left - asymmetric padding around the mask, in pixels.
  • insightface (optional) - skip it for plain inpainting, provide it to also get a control_image.

The outputs matter as much as the inputs, because this is a crop node built around pasting back later: resized_image and mask are what you actually generate on, control_image feeds InstantID's ControlNet, and x, y, original_width, original_height, new_width, new_height are the bookkeeping you need to composite the generated patch back into the full-size original once you're done. Don't discard those integer outputs - they're the return address, not debug noise.

Installing it

ComfyUI Manager: search comfyui-instantId-faceswap, install, restart. By hand: clone https://github.com/nosiu/comfyui-instantId-faceswap into custom_nodes, run pip install -r requirements.txt inside it, restart. The insightface input specifically needs the antelopev2 model files (five .onnx files) downloaded by hand from Hugging Face into ComfyUI/models/insightface/models/antelopev2 - this pack doesn't auto-fetch them, and skipping the download just means this node quietly stops producing a control_image rather than erroring, which is a friendlier failure mode than most. One thing worth flagging before you build around this: those antelopev2 weights are non-commercial research models, same restriction every InstantID- or InsightFace-based pipeline inherits - fine to build and run for yourself, not something to ship behind a paywall without a separate license.

Where people get burned

Model-path confusion is the recurring complaint on this specific pack - an early-adopter thread has people fighting to get InsightFace, ControlNet, and IPAdapter folders all recognized through extra_model_paths.yaml at once, with one user reporting ComfyUI refusing to start after adding an insightface: section. If your control_image comes out empty or this node errors on the insightface input, check that folder structure before anything else. Second, don't confuse resize_mode: auto with free - auto deliberately ignores your exact width/height and targets that pixel area instead, which surprises people expecting a literal 1024×1024 output every time. And if faces come out warped in the control_image, it's very likely the extreme-rotation issue called out above - run the source through Get Angle from Face → Rotate Image first.

CategoryInstantId Faceswap

Inputs (11)

NameTypeDefaultDescription
imageIMAGEPose image.
maskMASK
widthINT10240–16384
heightINT10240–16384
resize_modeCOMBO4 options: auto, free, scale by width, scale by height
upscale_methodCOMBO5 options: nearest-exact, bilinear, area, bicubic, lanczos
pad_topINT1000–16384
pad_rightINT1000–16384
pad_bottomINT1000–16384
pad_leftINT1000–16384
insightfaceoptINSIGHTFACE_APP

Outputs (9)

NameTypeDescription
resized_imageIMAGE
maskMASK
control_imageIMAGE
xINT
yINT
original_widthINT
original_heightINT
new_widthINT
new_heightINT