Created
October 31, 2023 00:17
-
-
Save lalunamel/6b582f865d2be881a501c574a136ce69 to your computer and use it in GitHub Desktop.
A custom ComfyUI node to produce a width and height from a pixel density and aspect ratio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
class ImageSizer: | |
def __init__(self): | |
pass | |
@classmethod | |
def INPUT_TYPES(s): | |
return { | |
"required": { | |
"model_type": (["SD","SDXL"],), | |
"aspect_ratio_width": ("INT",{ | |
"default": 1, | |
"step":1, | |
"display": "number" | |
}), | |
"aspect_ratio_height": ("INT",{ | |
"default": 1, | |
"step":1, | |
"display": "number" | |
}) | |
} | |
} | |
RETURN_TYPES = ("INT","INT") | |
RETURN_NAMES = ("Width", "Height") | |
FUNCTION = "run" | |
CATEGORY = "CodyCustom" | |
def run(self, model_type, aspect_ratio_width, aspect_ratio_height): | |
# Define the total pixel counts for SD and SDXL | |
total_pixels = { | |
'SD': 512 * 512, | |
'SDXL': 1024 * 1024 | |
} | |
# Calculate the number of total pixels based on model type | |
pixels = total_pixels.get(model_type, 0) | |
# Calculate the aspect ratio decimal | |
aspect_ratio_decimal = aspect_ratio_width / aspect_ratio_height | |
# Calculate width and height | |
width = math.sqrt(pixels * aspect_ratio_decimal) | |
height = pixels / width | |
# Return the width and height as a tuple of integers | |
return (int(round(width)), int(round(height))) | |
NODE_CLASS_MAPPINGS = { | |
"ImageSizer": ImageSizer | |
} | |
NODE_DISPLAY_NAME_MAPPINGS = { | |
"ImageSizer": "Image Sizer" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment