From ae27988af8b5d723f0342d1ae48c1062c634f6b0 Mon Sep 17 00:00:00 2001
From: thot experiment <94414189+thot-experiment@users.noreply.github.com>
Date: Thu, 1 May 2025 14:22:41 -0700
Subject: [PATCH] add metadata saving to SVG (#102)
---
comfy_api_nodes/nodes_recraft.py | 46 +++++++++++++++++++++++---------
1 file changed, 33 insertions(+), 13 deletions(-)
diff --git a/comfy_api_nodes/nodes_recraft.py b/comfy_api_nodes/nodes_recraft.py
index 0b98f8eb7..c769923ed 100644
--- a/comfy_api_nodes/nodes_recraft.py
+++ b/comfy_api_nodes/nodes_recraft.py
@@ -23,7 +23,7 @@ from comfy_api_nodes.apinode_utils import (
download_url_to_bytesio,
)
import folder_paths
-
+import json
import os
import torch
from io import BytesIO
@@ -70,22 +70,42 @@ class SaveSVGNode:
filename_prefix += self.prefix_append
full_output_folder, filename, counter, subfolder, filename_prefix = folder_paths.get_save_image_path(filename_prefix, self.output_dir)
results = list()
- for batch_number, svg_bytes in enumerate(svg.data):
- # NOTE: no way to do metadata for SVG right now, maybe figure this out later
- # 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]))
+ # Prepare metadata JSON
+ metadata_dict = {}
+ if prompt is not None:
+ metadata_dict["prompt"] = prompt
+ if extra_pnginfo is not None:
+ metadata_dict.update(extra_pnginfo)
+
+ # Convert metadata to JSON string
+ metadata_json = json.dumps(metadata_dict, indent=2) if metadata_dict else None
+
+ for batch_number, svg_bytes in enumerate(svg.data):
filename_with_batch_num = filename.replace("%batch_num%", str(batch_number))
file = f"{filename_with_batch_num}_{counter:05}_.svg"
+
+ # Read SVG content
+ svg_bytes.seek(0)
+ svg_content = svg_bytes.read().decode('utf-8')
+
+ # Inject metadata if available
+ if metadata_json:
+ # Create metadata element with CDATA section
+ metadata_element = f"""
+
+
+"""
+ # Insert metadata after opening svg tag using regex
+ import re
+ svg_content = re.sub(r'(