commit 222960a4a7af62e7c847f1513afadcb6b55cd319 Author: Budi Hartono Date: Sat Jun 7 19:54:50 2025 +0700 Initial commit diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..68bc17f --- /dev/null +++ b/.gitignore @@ -0,0 +1,160 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..50742ee --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Budi Hartono + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..80849be --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# comfyui-aspect-ratio-presets +Custom ComfyUI nodes for generating empty latent tensors with preset and axis-based aspect ratios. diff --git a/aspect_ratio_nodes.py b/aspect_ratio_nodes.py new file mode 100644 index 0000000..409c1d0 --- /dev/null +++ b/aspect_ratio_nodes.py @@ -0,0 +1,104 @@ +# aspect_ratio_nodes.py + +import numpy as np +import modules.scripts as scripts +from modules import shared +from modules.ui import create_node, create_node_callbacks +from presets import PRESETS + +def _validate_dim(v): + if v <= 0: + raise ValueError("Dimension must be > 0") + if v % 8 != 0: + raise ValueError("Dimension must be divisible by 8") + +class EmptyLatentAspectPreset(scripts.Script): + title = "Empty Latent (Aspect Ratio Preset)" + ui_category = "ComfyUI Aspect Ratio Preset" + + def ui(self): + self.resolution = self.ui.dropdown( + label="Resolution Preset", + choices=[f"{m} {w}×{h} ({lbl})" for m, lbl, w, h in PRESETS], + default=0, + tooltip="Choose a predefined resolution + aspect ratio" + ) + self.batch_size = self.ui.int_field( + label="Batch Size", + default=1, + minimum=1, + tooltip="How many blank latents to produce" + ) + + def run(self, p): + model, lbl, W, H = PRESETS[self.resolution] + _validate_dim(W); _validate_dim(H) + B = self.batch_size + C = shared.sd_model.cond_stage_model.latent_channels # usually 4 + shape = (B, C, H // 8, W // 8) + return np.zeros(shape, dtype=np.float32) + + +class EmptyLatentAspectByAxis(scripts.Script): + title = "Empty Latent (Aspect Ratio by Axis)" + ui_category = "ComfyUI Aspect Ratio Preset" + + ASPECT_CHOICES = [ + ("1:1 Square", (1,1)), + ("3:2 Landscape", (3,2)), + ("2:3 Portrait", (2,3)), + ("4:3 Landscape", (4,3)), + ("3:4 Portrait", (3,4)), + ("16:9 Landscape", (16,9)), + ("9:16 Portrait", (9,16)), + ("5:4 Landscape", (5,4)), + ("4:5 Portrait", (4,5)), + ("21:9 Widescreen", (21,9)), + ("9:21 Portrait", (9,21)), + ("7:5 Landscape", (7,5)), + ("5:7 Portrait", (5,7)), + ] + + def ui(self): + self.primary = self.ui.int_field( + label="Primary Dimension", + default=512, + tooltip="Your value for the chosen Reference Axis" + ) + self.axis = self.ui.dropdown( + label="Reference Axis", + choices=["Width","Height"], + default="Width", + tooltip="Which side does the Primary Dimension refer to?" + ) + self.aspect = self.ui.dropdown( + label="Aspect Ratio", + choices=[lbl for lbl,_ in self.ASPECT_CHOICES], + default=0, + tooltip="Select X:Y ratio and orientation" + ) + self.batch_size = self.ui.int_field( + label="Batch Size", + default=1, + minimum=1, + tooltip="How many blank latents to produce" + ) + + def run(self, p): + val = self.primary + _validate_dim(val) + w_ratio, h_ratio = self.ASPECT_CHOICES[self.aspect][1] + + if self.axis == "Width": + W = val + H = round(val * (h_ratio / w_ratio)) + else: + H = val + W = round(val * (w_ratio / h_ratio)) + + _validate_dim(W); _validate_dim(H) + + B = self.batch_size + C = shared.sd_model.cond_stage_model.latent_channels + shape = (B, C, H // 8, W // 8) + return np.zeros(shape, dtype=np.float32) \ No newline at end of file diff --git a/presets.py b/presets.py new file mode 100644 index 0000000..ed84220 --- /dev/null +++ b/presets.py @@ -0,0 +1,27 @@ +# presets.py + +PRESETS = [ + # model, label, w, h + ("SD 1.5", "1:1 Square", 512, 512), + ("SD 1.5", "3:2 Landscape", 768, 512), + ("SD 1.5", "4:3 Landscape", 768, 576), + ("SD 1.5", "16:9 Landscape",912, 512), + ("SD 1.5", "2:3 Portrait", 512, 768), + ("SD 1.5", "3:4 Portrait", 576, 768), + ("SD 1.5", "9:16 Portrait", 512, 912), + ("SDXL", "1:1 Square", 1024,1024), + ("SDXL", "3:2 Landscape", 1152,768), + ("SDXL", "4:3 Landscape", 1152,864), + ("SDXL", "16:9 Landscape",1360,768), + ("SDXL", "2:3 Portrait", 768,1152), + ("SDXL", "3:4 Portrait", 864,1152), + ("SDXL", "9:16 Portrait", 768,1360), + ("Flux/HiD","3:2 Landscape", 1216,832), + ("Flux/HiD","2:3 Portrait", 832,1216), + ("Flux/HiD","1:1 Square", 1408,1408), + ("Flux/HiD","4:3 Landscape", 1664,1216), + ("Flux/HiD","16:9 Landscape",1920,1088), + ("Flux/HiD","9:21 Portrait", 960,2176), + ("HiDream", "1:1 Square", 1280,1280), + ("HiDream", "1:1 Square", 1536,1536), +] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..296d654 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +numpy \ No newline at end of file