Sign In

Automatically match Aspect Ratio of reference in ComfyUI

6

Automatically match Aspect Ratio of reference in ComfyUI

Suppose you wanted to utilize images that were not necessarily 512x512, 512x768, or 768x512, and so on.

While there are probably more elegant solutions or premade nodes to accomplish this, I made the following workflow with simple logic gates and math and hope to explain how it works so you can understand the process and achieve similar results for your projects.

The image dimensions are fetched via the Get Image Size custom node in ComfyUI Essentials.

The text nodes above are just to show the dimensions here for reference and are not needed in this process.

The width is then used as variable "a" and height is used as variable "b" in three nodes. The bottom left node is our Resolution Constant which is just an Integer Primitive node. This lets us set the number that will be used for the smaller side of our generations. I work in SD1.5 so I set this to 512.

From the top to the bottom, the first node checks if the width is less than or equal to the height. If so, it sends out a TRUE signal from the BOOLEAN output. This signal will be used later.

The middle node takes the width (3072) divided by the resolution constant (512), which gives us a value of 6 in this example. Then the height (2048) is divided by the result (6), giving us 341.33... which we just use the integer output to say 341.

The bottom node is the same process, but instead taking the height (2048) divided by the resolution constant (512) for a result of (4). The width is divided by the result (4), giving us 768.

The original aspect ratio is that the width is 1.6 times the height, or a 3:2 ratio. This is the same ratio we get comparing the two results inside the parenthesis of the formulas.

Now that we know we've got 341.33 and 768 as results, we want the larger of the two numbers to be used as the larger dimension compared to our constant of 512, which would be 768 in this case.

Those two nodes to the right of the three nodes mentioned previously are "Switch Any" nodes from the Crystools set of custom nodes. We set the boolean signal from a widget to an input and then feed the boolean signal from the top node out to each of these two. Now when the width is less than or equal to the height, we have a TRUE signal meaning the values we put into the "on_true" inputs will be passed through to the right.

For the top one of these two nodes, labeled "Width", we set the resolution constant to be on_true and then our "Width compared to constant" formula output to on_false. Then we connect the resolution constant to on_false for our "Height" node and the "Height compared to constant" formula to on_true. So now between these two switches, 512 will be output in one when true and the other when false, giving us our smaller side while the larger formula output gets used as the opposing value on the switch.

Here's an example using a 1120x620 image. Workflow is in the attachments.

6

Comments