Nodes/ComfyUI_Swwan/ImageScaleByAspectRatio V2 (Swwan) · 旧版兼容
ComfyUI Node

ImageScaleByAspectRatio V2 (Swwan) · 旧版兼容

The LayerStyle aspect node, and why you got black bars

By aining2022·Created 10 months ago·Updated about 18 hours ago· 33
ImageScaleByAspectRatio V2 (Swwan) · 旧版兼容
  • image
  • mask
  • image
  • mask
  • original_size
  • width
  • height
◄aspect_ratio▾►
◄proportional_width1►
◄proportional_height1►
◄fit▾►
◄method▾►
◄round_to_multiple▾►
◄scale_to_side▾►
◄scale_to_length1024►
◄background_color#000000►

What it is

An aspect-ratio-driven resize: you declare a shape (16:9, 1:1, original) and a size rule, and the node produces an image with those proportions, padding or cropping as needed to get there. It's the "give me a 1920-longest 9:16 for this vertical video" node.

This is the LayerStyle original (MIT, by chflame163), reimplemented in ComfyUI_Swwan and marked 旧版兼容 - the recommended replacement is ImageResizeKJv2Alternative, the pack's Resize Image (Swwan) node with aspect_ratio mode. But it still has its own following, because a five-output node that also tells you the original size is convenient wiring, and because it takes a mask alongside the image.

How the sizing math runs

aspect_ratio is the shape: nine presets, plus custom, which reads proportional_width / proportional_height (so 16:9 there means 16 and 9).

scale_to_side decides which dimension scale_to_length pins: longest, shortest, width, height, or total_pixel(kilo pixel). Read that last one carefully - the unit is kilo-pixels, so 1024 means roughly one megapixel, not 1024 pixels. That's the mode people reach for to keep VRAM predictable across a mixed folder of image shapes.

Then fit decides what happens to the leftovers, and this is where almost everyone gets burned:

  • letterbox pads with background_color.
  • crop throws the excess away.
  • fill stretches and distorts.

letterbox is the first entry, so it's the default, and background_color defaults to #000000. Which is to say: the default configuration of this node paints black bars onto your image. That's not corruption, that's letterboxing. There's an open issue against this pack from a user who upscaled a portrait and found a ~3px black band down one side, reproducible on this node and not on the KJ v2 alternative - if you're seeing slivers of black, set fit to crop or change the background colour, and stop looking for a bug.

method is the resampler (lanczos, bicubic, hamming, bilinear, box, nearest) and round_to_multiple snaps the result to 8/16/…/512 or None - pick 8 or 16 for latent work.

Inputs and outputs

Both image and mask are optional; you need at least one, and if you pass both they must already be the same size - the node logs an error and returns None for the image when they don't match, which shows up downstream as a confusing type complaint. The image and mask get the same treatment and come back as image and mask.

The other three outputs are the good part: original_size on a BOX type, plus width and height as integers. Wire width and height into an Empty Latent Image or a "scale to megapixels" consumer and your geometry stays in sync without retyping. One caution the pack's README is emphatic about: BOX, BBOX, IMAGE_BOUNDS and SEAM are different protocols and are not interchangeable. Don't plug original_size into an uncrop node that wants a BBOX.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/aining2022/ComfyUI_Swwan
cd ComfyUI_Swwan
python -m pip install -r requirements.txt

Restart and hard-refresh. The pack reimplements the LayerStyle algorithm rather than calling it, so you do not need ComfyUI_LayerStyle installed - and you shouldn't install it alongside this pack if you care about ComfyUI Manager's conflict warnings, since the two overlap by name.

Common issues

Black edges or bars. fit=letterbox plus background_color=#000000. Change one of them.

Everything returns None. The mask didn't match the image dimensions, or neither input was connected - both paths log an error to the ComfyUI console before failing. Check the terminal, the node prints its own reason.

Unexpected multi-megapixel output. You used total_pixel and treated the number as pixels. It's kilo-pixels.

CategorySwwan/Legacy

Inputs (11)

NameTypeDefaultDescription
aspect_ratioCOMBO9 options: original, custom, 1:1, 3:2, 4:3, 16:9, +3
proportional_widthINT11–100000000—
proportional_heightINT11–100000000—
fitCOMBO3 options: letterbox, crop, fill
methodCOMBO6 options: lanczos, bicubic, hamming, bilinear, box, nearest
round_to_multipleCOMBO8 options: 8, 16, 32, 64, 128, 256, +2
scale_to_sideCOMBO6 options: None, longest, shortest, width, height, total_pixel(kilo pixel)
scale_to_lengthINT10244–100000000—
background_colorSTRING#000000—
imageoptIMAGE—
maskoptMASK—

Outputs (5)

NameTypeDescription
imageIMAGE—
maskMASK—
original_sizeBOX—
widthINT—
heightINT—