ADD 增加任意保存图片节点

This commit is contained in:
2025-07-28 18:14:25 +08:00
parent a46b76f76d
commit 5b0e970b6f
2 changed files with 72 additions and 3 deletions

View File

@@ -191,3 +191,70 @@ class SaveImageWithOutput:
counter = 1
return full_output_folder, filename_prefix, counter, subfolder, filename_prefix
class SaveImageAnywhere:
"""保存图片并输出的节点:既保存图片又能继续传递数据"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"images": ("IMAGE",),
"filename_prefix": ("STRING", {"default": "ComfyUI"}),
"output_dir": ("STRING", {"default": "output"}),
},
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("file_path",)
FUNCTION = "save_image"
OUTPUT_NODE = True
CATEGORY = "不忘科技-自定义节点🚩/图片"
def save_image(self, images, filename_prefix="ComfyUI", output_dir="output"):
full_output_folder, filename, counter, subfolder, filename_prefix = self.get_save_image_path(
filename_prefix,output_dir)
file_paths = []
for i, image in enumerate(images):
# 转换图片数据
img_array = 255. * image.cpu().numpy()
img = Image.fromarray(np.clip(img_array, 0, 255).astype(np.uint8))
# 生成文件名
file = f"{filename_prefix}_{counter + i:05}_.png"
file_path = os.path.join(full_output_folder, file)
# 保存图片
img.save(file_path, compress_level=4)
file_paths.append(file_path)
print(f"✅ 图片已保存到: {file_paths if file_paths else '未知路径'}")
return (file_paths[0] if file_paths else "",)
def get_save_image_path(self, filename_prefix, output_dir):
def map_filename(filename):
prefix_len = len(os.path.basename(filename_prefix))
prefix = filename[:prefix_len + 1]
try:
digits = int(filename[prefix_len + 1:].split('_')[0])
except:
digits = 0
return (digits, prefix)
subfolder = ""
full_output_folder = os.path.join(output_dir, subfolder)
try:
counter = max(filter(lambda a: a[1][:-1] == filename_prefix and a[1][-1] == "_",
map(map_filename, os.listdir(full_output_folder))))[0] + 1
except ValueError:
counter = 1
except FileNotFoundError:
os.makedirs(full_output_folder, exist_ok=True)
counter = 1
return full_output_folder, filename_prefix, counter, subfolder, filename_prefix