commit dc6d608ebf041743ffe4f0a92cf7e9decd058346 Author: Hamilton Date: Thu Nov 23 02:20:42 2023 -0800 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e928de1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..24c3ab8 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Comfy Fit Size + +A simple set of nodes for making an image fit within a bounding box + +## Author + +Hamilton Cline https://hdraws.com \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..0bf49be --- /dev/null +++ b/__init__.py @@ -0,0 +1,15 @@ +from .nodes import FitSize, FitSizeFromImage + +# A dictionary that contains all nodes you want to export with their names +# NOTE: names should be globally unique +NODE_CLASS_MAPPINGS = { + "FitSizeByMax": FitSize, + "FitSizeFromImage": FitSizeFromImage, +} + +NODE_DISPLAY_NAME_MAPPINGS = { + "FitSizeByMax": "Fit Size", + "FitSizeFromImage": "Fit Size From Image", +} + +__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS'] \ No newline at end of file diff --git a/nodes.py b/nodes.py new file mode 100644 index 0000000..bb4d701 --- /dev/null +++ b/nodes.py @@ -0,0 +1,71 @@ + + + + +def get_max_size (width, height, max): + aspect_ratio = width / height + + fit_width = max + fit_height = max + + print(aspect_ratio, fit_width, fit_height, max, width, height) + + if aspect_ratio > 1: + fit_height = int(max / aspect_ratio) + else: + fit_width = int(max * aspect_ratio) + + return (fit_width, fit_height, aspect_ratio) + +def get_image_size(IMAGE) -> tuple[int, int]: + samples = IMAGE.movedim(-1, 1) + size = samples.shape[3], samples.shape[2] + return size + +class FitSize: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "original_width": ("INT", {}), + "original_height": ("INT", {}), + "max_size": ("INT", {"default": 768, "step": 8}), + } + } + + RETURN_TYPES = ("INT", "INT", "FLOAT") + RETURN_NAMES = ("Fit Width", "Fit Height", "Aspect Ratio") + FUNCTION = "fit_to_size" + + CATEGORY = "Fitsize" + + def fit_to_size (self, original_width, original_height, max_size): + values = get_max_size(original_width, original_height, max_size) + return values + +class FitSizeFromImage: + def __init__(self): + pass + + @classmethod + def INPUT_TYPES(s): + return { + "required": { + "image": ("IMAGE",), + "max_size": ("INT", {"default": 768, "step": 8}), + } + } + + RETURN_TYPES = ("INT", "INT", "FLOAT") + RETURN_NAMES = ("Fit Width", "Fit Height", "Aspect Ratio") + FUNCTION = "fit_to_size_from_image" + + CATEGORY = "Fitsize" + + def fit_to_size_from_image (self, image, max_size): + size = get_image_size(image) + values = get_max_size(size[0], size[1], max_size) + return values \ No newline at end of file