🍒Image_Square_Pad⬜图片正方形填充
Pad to square, zero pixel loss
- image
- alpha
- image
Every image model wants squares. Your images aren't. Image_Square_Pad fixes that the non-destructive way: it extends the shorter side so the canvas becomes a square, centering the original and filling the gap with transparency or a color - without scaling, cropping, or touching a single pixel of your content. The author's own docstring describes it as "similar to Photoshop's canvas size function," and that's exactly right: it's the canvas that grows, not the image.
Why you'll reach for it: square-cropping a portrait loses content; squishing it distorts; this pads it. Feed the result to a square-latent pipeline, a tile-based upscaler that wants square inputs, or a model that rejects non-square resolutions, and you keep every pixel of the original. The README example shows exactly that - a wide banner image becoming a square without losing the sides.
How it works
This is one of the few nodes in the pack that's pure tensor math - no PIL round-trip, no cv2. It takes the image tensor, computes the long edge, creates a new square tensor filled with your background, and copies the original into the center. Because it never re-encodes the pixels, it's genuinely lossless.
The alpha input (a MASK) lets you bring in an existing alpha channel: it gets merged into an RGBA output, with invert_alpha (default on) flipping the mask first - the tooltip says "usually needs to be enabled," because ComfyUI masks are typically inverted relative to what you'd expect as opacity. The color input takes a hex string like FF0000 or #FF0000; leave it empty and the padding is transparent. If your image is already square, the node returns it unchanged - no wasted work.
The inputs that matter
image- what to pad.color- hex fill for the pad; empty = transparent.alpha+invert_alpha- bring in transparency from a mask when you need RGBA output.- Everything else is automatic - it always centers, always uses the long edge.
Output: image, the padded square (RGB if no alpha/color, RGBA otherwise).
Installation
Standard KimNodes pack install - Manager → search "ComfyUI_KimNodes" → Install → Restart, or git clone https://github.com/wjl0313/ComfyUI_KimNodes into custom_nodes. No extra dependencies - it's torch/numpy only.
Common issues
- Transparent padding shows up black downstream - you're getting RGBA but a downstream node that ignores alpha renders the transparent area black. Either feed the result into something that handles alpha, or set
colorto a solid hex likeFFFFFF. - The mask looks inverted - that's the
invert_alphadefault (on) doing its job. If your alpha appears wrong, flipinvert_alphaoff. - "Invalid hex format" fallback - a malformed color string (e.g. 3-digit hex or missing letters) makes the node fall back to transparent rather than erroring. Use 6 hex digits.
For anything that needs square inputs without pixel loss, this is a quiet workhorse - and it behaves exactly the way you'd hope a canvas-padding node behaves.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| alphaopt | MASK | 图片的透明度信息,如果提供将与图片合并 | |
| invert_alphaopt | BOOLEAN | true | 是否反转alpha值,通常需要开启 |
| coloropt | STRING | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | — |