added helper.py with save to absolte path node + added requirements

This commit is contained in:
njlent
2025-02-13 16:47:25 +01:00
parent 2f8504eb25
commit ec3d4d0d76
6 changed files with 132 additions and 3 deletions
+5 -2
View File
@@ -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 = """
Binary file not shown.
Binary file not shown.
Binary file not shown.
+122
View File
@@ -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"
}
+5 -1
View File
@@ -3,4 +3,8 @@ googlesearch-python
requests
bs4
jax
gallery-dl
gallery-dl
numpy
Pillow
json-repair