Skip to content

add metadata nodes #8737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions comfy_extras/nodes_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import os
import numpy as np
import folder_paths
from PIL import Image
from PIL.PngImagePlugin import PngInfo
from comfy.cli_args import args
import json

class GetWorkflowMetadata:
@classmethod
def INPUT_TYPES(s):
return {
"hidden": {
"extra_pnginfo": "EXTRA_PNGINFO"
},
}
RETURN_TYPES = ("METADATA",)
FUNCTION = "metaget"
CATEGORY = "image"
DESCRIPTION = "Gets the workflow metadata from the current workflow."
def metaget(self, extra_pnginfo):
metadata = PngInfo()
if not args.disable_metadata:
if extra_pnginfo is not None:
for x in extra_pnginfo:
metadata.add_text(x, json.dumps(extra_pnginfo[x]))
return (metadata,)

class EmptyMetadata:
@classmethod
def INPUT_TYPES(s):
return {
}
RETURN_TYPES = ("METADATA",)
FUNCTION = "metaget"
CATEGORY = "image"
DESCRIPTION = "Create a blank / empty metadata to add upon."
def metaget(self):
metadata = PngInfo()
return (metadata,)

class AddMetadataValue:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"key": ("STRING", {"tooltip": "Key to save the value at."}),
"value": ("STRING", {"tooltip": "What to add to the metadata."}),
"metadata": ("METADATA", {"tooltip": "Metadata to add to."})
}
}
RETURN_TYPES = ("METADATA",)
FUNCTION = "metaadd"
CATEGORY = "image"
DESCRIPTION = "Add an arbitrary value to the metadata."
def metaadd(self, key, value, metadata):
metadata.add_text(key, json.dumps(value))
return (metadata,)



class SaveImageCustomMetadata:
def __init__(self):
self.output_dir = folder_paths.get_output_directory()
self.type = "output"
self.prefix_append = ""
self.compress_level = 4

@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."}),
"metadata": ("METADATA", {"tooltip": "Metadata to save with the image"})
}
}

RETURN_TYPES = ()
FUNCTION = "save_images"

OUTPUT_NODE = True

CATEGORY = "image"
DESCRIPTION = "Saves the input images to your ComfyUI output directory with custom metadata."

def save_images(self, images, filename_prefix="ComfyUI", metadata=None):
filename_prefix += self.prefix_append
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])
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))
filename_with_batch_num = filename.replace("%batch_num%", str(batch_number))
file = f"{filename_with_batch_num}_{counter:05}_.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 } }



NODE_CLASS_MAPPINGS = {
"GetWorkflowMetadata": GetWorkflowMetadata,
"EmptyMetadata": EmptyMetadata,
"AddMetadataValue": AddMetadataValue,
"SaveImageCustomMetadata": SaveImageCustomMetadata
}
NODE_DISPLAY_NAME_MAPPINGS = {
"Get Workflow Metadata": "GetWorkflowMetadata",
"Empty Metadata": "EmptyMetadata",
"Add Metadata Value": "AddMetadataValue",
"Save Image with custom metadata": "SaveImageCustomMetadata"
}
1 change: 1 addition & 0 deletions nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2283,6 +2283,7 @@ def init_builtin_extra_nodes():
"nodes_string.py",
"nodes_camera_trajectory.py",
"nodes_edit_model.py",
"nodes_metadata.py",
]

import_failed = []
Expand Down