commit 851656b42e8f42f4a2c9050e203484fc705e6465 Author: bradsec Date: Fri Aug 18 16:12:24 2023 +1000 Initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0074bf8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 BRADSEC + +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..470f3a3 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +# Custom Node - ResolutionSelector for ComfyUI + +Custom image resolution helper node for [ComfyUI](https://github.com/comfyanonymous/ComfyUI) + +- Select base SDXL resolution, width and height are returned as INT values. +- If using older model SD 1.5 or SD 2.1 use the base adjustment dropdown. This will adjust (reduce) the base_resolution to suit the model. ie. 1024 (SDXL) will equal 768 in SD21 and 512 in SD15. +- Resolution list based off what is currently being used in the [Fooocus SDXL Web UI](https://github.com/lllyasviel/Fooocus) + +### Installation + +``` +# Change to the directory you installed ComfyUI +cd pathTo/ComfyUI + +# Change to the custom_nodes directory +cd custom_nodes + +git clone https://github.com/bradsec/ComfyUI_ResolutionSelector.git + +# Restart ComfyUI +``` + +- The new node can be found under: +`Add Node > utils > Resolution Selector (w x h)` + + + +![image](resolutionselector_node.png) \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..f5a9097 --- /dev/null +++ b/__init__.py @@ -0,0 +1,3 @@ +from .resolution_selector import NODE_CLASS_MAPPINGS, NODE_DISPLAY_NAME_MAPPINGS + +__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] \ No newline at end of file diff --git a/resolution_selector.py b/resolution_selector.py new file mode 100644 index 0000000..7629345 --- /dev/null +++ b/resolution_selector.py @@ -0,0 +1,68 @@ + +# Define resolution presets +# Resolutions which appear to work well for SDXL as used in https://github.com/lllyasviel/Fooocus +BASE_RESOLUTIONS = [(1024, 1024), (704, 1408), (704, 1344), (768, 1344), (768, 1280), (832, 1216), (832, 1152), (896, 1152), (896, 1088), (960, 1088), (960, 1024), (1024, 960), (1088, 960), (1088, 896), (1152, 896), (1152, 832), (1216, 832), (1280, 768), (1344, 768), (1344, 704), (1408, 704), (1472, 704), (1536, 640), (1600, 640), (1664, 576), (1728, 576)] + +# Create a list of resolution strings for the drop-down menu +resolution_strings = [f"{width}x{height}" for width, height in BASE_RESOLUTIONS] + +# Create the custom node class +class ResolutionSelector: + """ + A node to provide a drop-down list of resolutions and returns two int values (width and height). + """ + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(s): + """ + Return a dictionary which contains config for all input fields. + """ + return { + "required": { + "base_resolution": (resolution_strings,), + "base_adjustment": (["SDXL (None)", "SD21 (75%)", "SD15 (50%)"],) + } + } + + RETURN_TYPES = ("INT", "INT") + RETURN_NAMES = ("width", "height") + FUNCTION = "select_resolution" + CATEGORY = 'utils' + + @staticmethod + def select_resolution(base_resolution, base_adjustment): + """ + Returns the width and height based on the selected resolution and adjustment. + + Args: + base_resolution (str): Selected resolution in the format "widthxheight". + base_adjustment (str): Selected adjustment (resolution values reduction) based on Stable Diffusion version. + + Returns: + Tuple[int, int]: Adjusted width and height. + """ + try: + width, height = map(int, base_resolution.split('x')) + except ValueError: + raise ValueError("Invalid base_resolution format.") + + adjustment_factors = {"SDXL (None)": 1, "SD21 (75%)": 0.75, "SD15 (50%)": 0.5} + factor = adjustment_factors.get(base_adjustment) + + if factor is None: + raise ValueError("Invalid base_adjustment value.") + + width = int(width * factor) + height = int(height * factor) + + return width, height + +NODE_CLASS_MAPPINGS = { + "ResolutionSelector": ResolutionSelector, +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "ResolutionSelector": "Resolution Selector (w x h)", +} \ No newline at end of file diff --git a/resolutionselector_node.png b/resolutionselector_node.png new file mode 100644 index 0000000..b0fd849 Binary files /dev/null and b/resolutionselector_node.png differ