Nodes/ComfyUI-YCNodes/Image Aspect Expand
ComfyUI Node

Image Aspect Expand

Pad an image to a target ratio — and get the mask that proves what's original

By yichengup·Created 2 years ago·Updated 4 months ago· 47
Image Aspect Expand
  • image
  • expanded_image
  • original_mask
  • width
  • height
  • selected_ratio
aspect_ratios16:9 9:16 4:3 3:4 1:1
fill_color#000000
alignmentcenter

Outpainting works a lot better when you prepare the canvas properly. The trick isn't just telling the model "extend this image" - it's padding the image to the target ratio yourself, in a solid color, and handing the model a mask that says this region is sacred and that region is empty space to invent. ImageAspectExpand is the padding half of that recipe. It takes an image, expands the canvas to a ratio (or an absolute size) you choose, fills the new area with a color, and outputs the expanded image plus a mask marking exactly where the original pixels are.

How it works

You give it a list of target ratios (or sizes), one per line, and it picks whichever is closest to your image's current aspect ratio. Then it computes the smallest canvas at that ratio that can hold the original, paints the background color, and pastes the image at a position you control (alignment - center, top, bottom, left, right, or the four corners). The original region is white in the output mask; everything added is black.

Two modes are worth understanding. With a ratio like 16:9, it just pads (no scaling - the original pixels are untouched, which is the point for outpainting). With an absolute size like 768x1024, it first scales the image down to fit inside that box (LANCZOS, preserving aspect), then pads the remainder. The ratio mode is your outpainting friend; the size mode is more of a "fit into a fixed slot" tool.

The inputs that matter

  • aspect_ratios - multiline, one per line. Ratios (4:3) or absolute sizes (768x1024) both parse; with several entries it auto-selects the closest. Default: 16:9 / 9:16 / 4:3 / 3:4 / 1:1.
  • fill_color - hex color for the padding. Black is the classic outpainting choice, but white matches where the model expects a bright canvas.
  • alignment - where the original sits in the expanded canvas.

Outputs: expanded_image, original_mask (the sacred region), plus width, height, and selected_ratio as plain values you can wire into an EmptyLatent or log.

The canonical workflow

Expand to 16:9 with black padding → feed the mask to inpainting conditioning → let the model outpaint the black band. The KB's inpainting essay keeps hammering that unpadded, uncomposited passes accumulate drift and trash images; this node is the "padded and composited" version done in two nodes instead of six. Because original_mask is produced alongside the image, you never have to rebuild the mask by hand, which is where these workflows usually fall apart.

Installing it

Part of ComfyUI-YCNodes. ComfyUI Manager → search ComfyUI-YCNodes → install, or:

cd ComfyUI/custom_nodes
git clone https://github.com/yichengup/ComfyUI-YCNodes

Restart. Needs PIL and torch from the pack's standard requirements - nothing special.

Gotchas

The auto-pick behavior is a trap if your list has close neighbors: a 1.0-aspect image fed a list containing both 4:3 and 3:2 will quietly choose whichever is numerically nearest, and you may not notice. Keep the list to ratios you actually intend to use, or use a single line to force one. Also remember the ratio mode never scales - if your source is 0.9-aspect and you ask for 1:1, the canvas grows to fit but the original stays 0.9, so the padding will be uneven side-to-side. That's correct behavior; it just surprises people the first time.

CategoryYCNode/Image

Inputs (4)

NameTypeDefaultDescription
imageIMAGE
aspect_ratiosSTRING16:9 9:16 4:3 3:4 1:1比例或尺寸列表,每行一个。格式: 比例(如4:3) 或 具体尺寸(如768x1024)。多行时自动选择最接近的
fill_colorSTRING#000000填充颜色(十六进制)
alignmentCOMBOcenter原图对齐方式

Outputs (5)

NameTypeDescription
expanded_imageIMAGE
original_maskMASK
widthINT
heightINT
selected_ratioSTRING