diff --git a/__init__.py b/__init__.py index 966c851..bd346d2 100644 --- a/__init__.py +++ b/__init__.py @@ -1,13 +1,16 @@ from .polymath import Polymath, MediaScraper +from .helper import SaveAbs NODE_CLASS_MAPPINGS = { "polymath_chat": Polymath, - "polymath_scraper": MediaScraper + "polymath_scraper": MediaScraper, + "SaveAbsolute": SaveAbs } NODE_DISPLAY_NAME_MAPPINGS = { "polymath_chat": "LLM Polymath Chat with Advanced Web and Link Search", - "polymath_scraper": "LLM Polymath Scraper for various sites" + "polymath_scraper": "LLM Polymath Scraper for various sites", + "SaveAbsolute": "Save Image to Absolute Path" } ascii_art = """ diff --git a/__pycache__/__init__.cpython-312.pyc b/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000..ced84f9 Binary files /dev/null and b/__pycache__/__init__.cpython-312.pyc differ diff --git a/__pycache__/helper.cpython-312.pyc b/__pycache__/helper.cpython-312.pyc new file mode 100644 index 0000000..3e24a3a Binary files /dev/null and b/__pycache__/helper.cpython-312.pyc differ diff --git a/__pycache__/polymath.cpython-312.pyc b/__pycache__/polymath.cpython-312.pyc new file mode 100644 index 0000000..ba10415 Binary files /dev/null and b/__pycache__/polymath.cpython-312.pyc differ diff --git a/helper.py b/helper.py new file mode 100644 index 0000000..6edd597 --- /dev/null +++ b/helper.py @@ -0,0 +1,122 @@ +import numpy as np +from PIL import Image +from PIL.PngImagePlugin import PngInfo +import base64,os,random +import folder_paths +import json,io +from comfy.cli_args import args +import math,glob + +class SaveAbs: + def __init__(self): + self.output_dir = folder_paths.get_output_directory() + self.type = "output" + self.compress_level = 4 + + @classmethod + def INPUT_TYPES(s): + return {"required": + {"images": ("IMAGE", ), + "file_path": ("STRING",{"multiline": True,"default": "","dynamicPrompts": False}), + }, + "hidden": {"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"}, + + } + + RETURN_TYPES = () + FUNCTION = "save_images" + + OUTPUT_NODE = True + + CATEGORY = "Polymath/helper" + + def save_images(self, images,file_path , prompt=None, extra_pnginfo=None): + filename_prefix = os.path.basename(file_path) + if file_path=='': + filename_prefix="ComfyUI" + + filename_prefix, _ = os.path.splitext(filename_prefix) + + _, extension = os.path.splitext(file_path) + + if extension: + # 是文件名,需要处理 + file_path=os.path.dirname(file_path) + # filename_prefix= + + + full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix, self.output_dir, images[0].shape[1], images[0].shape[0]) + + + if not os.path.exists(file_path): + # 使用os.makedirs函数创建新目录 + os.makedirs(file_path) + print("dir created") + else: + print("dir already exists") + + # 使用glob模块获取当前目录下的所有文件 + if file_path=="": + files = glob.glob(full_output_folder + '/*') + else: + files = glob.glob(file_path + '/*') + # 统计文件数量 + file_count = len(files) + counter+=file_count + print('number of files',file_count,counter) + + results = list() + for image in images: + i = 255. * image.cpu().numpy() + img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8)) + metadata = None + if not args.disable_metadata: + metadata = PngInfo() + if prompt is not None: + metadata.add_text("prompt", json.dumps(prompt)) + if extra_pnginfo is not None: + for x in extra_pnginfo: + metadata.add_text(x, json.dumps(extra_pnginfo[x])) + + file = f"{filename}_{counter:05}.png" + + if file_path=="": + fp=os.path.join(full_output_folder, file) + if os.path.exists(fp): + file = f"{filename}_{counter:05}_{generate_random_string(8)}.png" + fp=os.path.join(full_output_folder, file) + img.save(fp, pnginfo=metadata, compress_level=self.compress_level) + results.append({ + "filename": file, + "subfolder": subfolder, + "type": self.type + }) + + else: + + fp=os.path.join(file_path, file) + if os.path.exists(fp): + file = f"{filename}_{counter:05}_{generate_random_string(8)}.png" + fp=os.path.join(file_path, file) + + img.save(os.path.join(file_path, file), pnginfo=metadata, compress_level=self.compress_level) + results.append({ + "filename": file, + "subfolder": file_path, + "type": self.type + }) + counter += 1 + + return () + + +# A dictionary that contains all nodes you want to export with their names +# NOTE: names should be globally unique +NODE_CLASS_MAPPINGS = { + "SaveAbsolute": SaveAbs +} + +# A dictionary that contains the friendly/humanly readable titles for the nodes +NODE_DISPLAY_NAME_MAPPINGS = { + "SaveAbsolute": "Save Image to Absolute Path" +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 3a53046..fe6fd43 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,8 @@ googlesearch-python requests bs4 jax -gallery-dl \ No newline at end of file +gallery-dl + +numpy +Pillow +json-repair \ No newline at end of file