Applying a custom name format to file? #12827
-
|
I want all my save images to be named like so: |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
|
Save Image Extended should be able to do that. https://github.com/audioscavenger/save-image-extended-comfyui |
Beta Was this translation helpful? Give feedback.
-
|
From my point of view, custom naming belongs in a templated output policy rather than scattered one-off nodes, because sequence number, timestamp, and seed are all stable concepts users will want to combine in different orders. Once people start post-processing files just to rename them, that is a sign the save layer is too rigid. Even a small token-based format string with zero-padding support would probably solve most of this cleanly. |
Beta Was this translation helpful? Give feedback.
-
|
I ended up writing my own node for it, based on the built in SaveImage node: from PIL import Image
from PIL.PngImagePlugin import PngInfo
from comfy.cli_args import args
import folder_paths
import numpy as np
import json
import os
class MySaveImage:
def __init__(self):
self.output_dir = folder_paths.get_output_directory()
self.type = "output"
self.prefix_append = ""
self.compress_level = 4
u/classmethod
def INPUT_TYPES(s):
return {
"required": {
"images": ("IMAGE", {"tooltip": "The images to save."}),
"filename_prefix": ("STRING", {"default": "ComfyUI", "tooltip": "The prefix for the file to save. This may include formatting information such as %date:yyyy-MM-dd% or %Empty Latent Image.width% to include values from nodes."}),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff})
},
"hidden": {
"prompt": "PROMPT", "extra_pnginfo": "EXTRA_PNGINFO"
},
}
RETURN_TYPES = ()
FUNCTION = "save_images"
OUTPUT_NODE = True
CATEGORY = "image"
ESSENTIALS_CATEGORY = "Basics"
DESCRIPTION = "Saves the input images to your ComfyUI output directory."
SEARCH_ALIASES = ["save", "save image", "export image", "output image", "write image", "download"]
def save_images(self, images, filename_prefix, seed, prompt=None, extra_pnginfo=None):
filename_prefix += self.prefix_append
print(self.output_dir)
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])
counter = len([name for name in os.listdir(self.output_dir) if os.path.isfile(os.path.join(self.output_dir, name))])
results = list()
for (batch_number, image) in enumerate(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]))
filename_with_batch_num = filename.replace("%batch_num%", str(batch_number))
file = f"{counter:03} {filename_with_batch_num} {seed}.png"
img.save(os.path.join(full_output_folder, file), pnginfo=metadata, compress_level=self.compress_level)
results.append({
"filename": file,
"subfolder": subfolder,
"type": self.type
})
counter += 1
return { "ui": { "images": results } }
# A dictionary that contains all nodes you want to export with their names
# NOTE: names should be globally unique
NODE_CLASS_MAPPINGS = {
"MySaveImage": MySaveImage
}
# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
"MySaveImage": "My Save Image"
} |
Beta Was this translation helpful? Give feedback.
I ended up writing my own node for it, based on the built in SaveImage node: