Save Image with Metadata
This commit is contained in:
+24
-4
@@ -38,10 +38,10 @@ class Capture:
|
||||
if value is not None:
|
||||
format = field_data.get("format")
|
||||
v = value
|
||||
if format is not None:
|
||||
v = format(v)
|
||||
if isinstance(value, list) and len(value) > 0:
|
||||
v = value[0]
|
||||
if format is not None:
|
||||
v = format(v)
|
||||
inputs[meta].append((node_id, v))
|
||||
return inputs
|
||||
|
||||
@@ -52,9 +52,9 @@ class Capture:
|
||||
def update_pnginfo_dict(metafield, key):
|
||||
x = inputs.get(metafield, [])
|
||||
if len(x) > 0:
|
||||
pnginfo_dict[key] = x[-1][1]
|
||||
pnginfo_dict[key] = x[0][1]
|
||||
|
||||
update_pnginfo_dict(MetaField.POSITIVE_PROMPT, "")
|
||||
update_pnginfo_dict(MetaField.POSITIVE_PROMPT, "Positive prompt")
|
||||
update_pnginfo_dict(MetaField.NEGATIVE_PROMPT, "Negative prompt")
|
||||
update_pnginfo_dict(MetaField.STEPS, "Steps")
|
||||
|
||||
@@ -76,4 +76,24 @@ class Capture:
|
||||
if len(image_widths) > 0 and len(image_heights) > 0:
|
||||
pnginfo_dict["Size"] = f"{image_widths[0][1]}x{image_heights[0][1]}"
|
||||
|
||||
update_pnginfo_dict(MetaField.MODEL_NAME, "Model name")
|
||||
update_pnginfo_dict(MetaField.MODEL_HASH, "Model hash")
|
||||
|
||||
return pnginfo_dict
|
||||
|
||||
@classmethod
|
||||
def gen_parameters_str(cls, pnginfo_dict):
|
||||
result = pnginfo_dict.get("Positive prompt", "") + "\n"
|
||||
result += "Negative prompt: " + pnginfo_dict.get("Negative prompt", "") + "\n"
|
||||
|
||||
s_list = []
|
||||
pnginfo_dict_without_prompt = {
|
||||
k: v
|
||||
for k, v in pnginfo_dict.items()
|
||||
if k not in {"Positive prompt", "Negative prompt"}
|
||||
}
|
||||
for k, v in pnginfo_dict_without_prompt.items():
|
||||
s = str(v).strip().replace("\n", " ")
|
||||
s_list.append(f"{k}: {s}")
|
||||
|
||||
return result + ", ".join(s_list)
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
from .meta import MetaField
|
||||
from .validators import is_positive_prompt, is_negative_prompt
|
||||
from .formatters import calc_model_hash
|
||||
|
||||
|
||||
CAPTURE_FIELD_LIST = {
|
||||
"CheckpointLoaderSimple": {
|
||||
MetaField.MODEL_NAME: {"field_name": "ckpt_name"},
|
||||
MetaField.MODEL_HASH: {"field_name": "ckpt_name", "format": calc_model_hash},
|
||||
},
|
||||
"EmptyLatentImage": {
|
||||
MetaField.IMAGE_WIDTH: {"field_name": "width"},
|
||||
MetaField.IMAGE_HEIGHT: {"field_name": "height"},
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import hashlib
|
||||
|
||||
import folder_paths
|
||||
|
||||
cache_model_hash = {}
|
||||
|
||||
|
||||
def calc_model_hash(model_name):
|
||||
if model_name in cache_model_hash:
|
||||
return cache_model_hash[model_name]
|
||||
|
||||
filename = folder_paths.get_full_path("checkpoints", model_name)
|
||||
sha256_hash = hashlib.sha256()
|
||||
|
||||
with open(filename, "rb") as f:
|
||||
for byte_block in iter(lambda: f.read(4096), b""):
|
||||
sha256_hash.update(byte_block)
|
||||
model_hash = sha256_hash.hexdigest()[:10]
|
||||
|
||||
cache_model_hash[model_name] = model_hash
|
||||
return model_hash
|
||||
@@ -3,6 +3,7 @@ from enum import Enum
|
||||
|
||||
class MetaField(Enum):
|
||||
MODEL_NAME = 0
|
||||
MODEL_HASH = 1
|
||||
POSITIVE_PROMPT = 10
|
||||
NEGATIVE_PROMPT = 11
|
||||
SEED = 20
|
||||
|
||||
+10
-6
@@ -1,4 +1,5 @@
|
||||
import json
|
||||
import os
|
||||
|
||||
from PIL import Image
|
||||
from PIL.PngImagePlugin import PngInfo
|
||||
@@ -63,7 +64,10 @@ class SaveImageWithMetaData(BaseNode):
|
||||
metadata = None
|
||||
if not args.disable_metadata:
|
||||
metadata = PngInfo()
|
||||
# metadata.add_text("parameters", comment)
|
||||
if pnginfo_dict:
|
||||
metadata.add_text(
|
||||
"parameters", Capture.gen_parameters_str(pnginfo_dict)
|
||||
)
|
||||
if prompt is not None:
|
||||
metadata.add_text("prompt", json.dumps(prompt))
|
||||
if extra_pnginfo is not None:
|
||||
@@ -71,11 +75,11 @@ class SaveImageWithMetaData(BaseNode):
|
||||
metadata.add_text(x, json.dumps(extra_pnginfo[x]))
|
||||
|
||||
file = f"{filename}_{counter:05}_.png"
|
||||
# img.save(
|
||||
# os.path.join(full_output_folder, file),
|
||||
# pnginfo=metadata,
|
||||
# compress_level=self.compress_level,
|
||||
# )
|
||||
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}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user