├── .gitignore ├── LICENSE ├── README.md ├── __init__.py └── preview_subselection.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 underclockeddev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ComfyUI-PreviewSubselection-Node 2 | 3 | ![image](https://github.com/underclockeddev/ComfyUI-PreviewSubselection-Node/assets/71031886/1db60542-c681-49d3-8e49-0909034e01d2) 4 | 5 | ![image](https://github.com/underclockeddev/ComfyUI-PreviewSubselection-Node/assets/71031886/23489a83-0f28-4ea7-86ef-cfe7db68d226) 6 | 7 | ![image](https://github.com/underclockeddev/ComfyUI-PreviewSubselection-Node/assets/71031886/ef6bc44e-bfe9-45e1-8434-fb50f5a35e1e) 8 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | from .preview_subselection import PreviewSubselection 2 | 3 | NODE_CLASS_MAPPINGS = { 4 | "PreviewSubselection": PreviewSubselection, 5 | } 6 | 7 | NODE_DISPLAY_NAME_MAPPINGS = { 8 | "PreviewSubselection": "PreviewSubselection", 9 | } 10 | 11 | __version__ = "0.1.0" 12 | __all__ = ["NODE_CLASS_MAPPINGS", "NODE_DISPLAY_NAME_MAPPINGS"] 13 | -------------------------------------------------------------------------------- /preview_subselection.py: -------------------------------------------------------------------------------- 1 | from PIL import Image, ImageDraw, ImageFont 2 | import os 3 | import numpy as np 4 | from os.path import join 5 | import folder_paths 6 | import random 7 | import torch 8 | import torchvision.transforms as transforms 9 | import comfy.utils 10 | 11 | class PreviewSubselection: 12 | def __init__(self): 13 | self.output_dir = folder_paths.get_temp_directory() 14 | self.type = "temp" 15 | self.prefix_append = "_temp_" + ''.join(random.choice("abcdefghijklmnopqrstupvxyz") for x in range(5)) 16 | self.compress_level = 1 17 | 18 | @classmethod 19 | def INPUT_TYPES(self): 20 | return {"required":{ 21 | "total_width": ("INT", {"default": 512, "min": 64, "max": 8192, "step": 8}), 22 | "total_height": ("INT", {"default": 512, "min": 64, "max": 8192, "step": 8}), 23 | "width": ("INT", {"forceInput": True, "min": 64, "max": 8192, "step": 8}), 24 | "height": ("INT", {"forceInput": True, "min": 64, "max": 8192, "step": 8}), 25 | "x": ("INT", {"forceInput": True, "min": 0, "max": 8192, "step": 8}), 26 | "y": ("INT", {"forceInput": True, "min": 0, "max": 8192, "step": 8}), 27 | "subselection_color": ("STRING", {"default": "red"}), 28 | "background_color": ("STRING", {"default": "#444444"}), 29 | "label": ("STRING", {"default": ""}), 30 | "preview_method": (["Passthrough", "Render Here"],), 31 | } 32 | } 33 | 34 | RETURN_TYPES = ("IMAGE",) 35 | CATEGORY = "underclocked" 36 | OUTPUT_NODE = True 37 | FUNCTION = "show_preview" 38 | 39 | def show_preview(self, total_width, total_height, width, height, x, y, subselection_color, background_color, label, preview_method, filename_prefix="ComfyUI", prompt=None, extra_pnginfo=None): 40 | filename_prefix += self.prefix_append 41 | full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix, self.output_dir) 42 | results = list() 43 | 44 | myImage = Image.new("RGBA", (total_width, total_height)) 45 | draw = ImageDraw.Draw(myImage) 46 | draw.rectangle((0, 0, total_width, total_height), outline=background_color, fill=background_color, width=25) 47 | 48 | 49 | box = (x, y, x + width, y + height) 50 | draw.rectangle(box, outline=subselection_color, fill=subselection_color, width=0) 51 | 52 | text = label 53 | font_size = 100 54 | size = None 55 | while (size is None or size[0] > box[2] - box[0] or size[1] > box[3] - box[1]) and font_size > 0: 56 | font = ImageFont.truetype("cour.ttf", font_size) 57 | size = font.getsize_multiline(text) 58 | font_size -= 1 59 | draw.multiline_text((box[0], box[1]), text, "#000", font) 60 | 61 | im2arr = np.array(myImage) 62 | img = Image.fromarray(np.clip(im2arr, 0, 255).astype(np.uint8)) 63 | file = f"{filename}_.png" 64 | img.save(os.path.join(full_output_folder, file), compress_level=self.compress_level) 65 | results.append({ 66 | "filename": file, 67 | "subfolder": subfolder, 68 | "type": self.type 69 | }) 70 | transform = transforms.ToTensor() 71 | tensor = transform(myImage) 72 | result = list() 73 | result.append(torch.from_numpy(np.array(myImage).astype(np.float32) / 255.0).unsqueeze(0)) 74 | if preview_method == "Passthrough": 75 | return {"result": result} 76 | else: 77 | return {"ui": { "images": results }, "result": result} --------------------------------------------------------------------------------