V 0.6.0 - PNG to img recycler

This commit is contained in:
DESKTOP-CNFQ7PM\Primere
2024-06-18 11:09:03 +02:00
parent bcd8223733
commit fc4af4a8ec
2 changed files with 79 additions and 80 deletions
+7 -4
View File
@@ -61,10 +61,11 @@ class PrimereMetaSave:
"filename_number_start": ("BOOLEAN", {"default":False}),
"extension": (['png', 'jpeg', 'jpg', 'gif', 'tiff', 'webp'], {"default": "jpg"}),
"png_embed_workflow": ("BOOLEAN", {"default":False}),
"png_embed_data": ("BOOLEAN", {"default": False}),
"image_embed_exif": ("BOOLEAN", {"default":False}),
"quality": ("INT", {"default": 95, "min": 1, "max": 100, "step": 1}),
"overwrite_mode": (["false", "prefix_as_filename"],),
"save_mata_to_json": ("BOOLEAN", {"default": False}),
"save_meta_to_json": ("BOOLEAN", {"default": False}),
"save_info_to_txt": ("BOOLEAN", {"default": False}),
},
"optional": {
@@ -76,11 +77,11 @@ class PrimereMetaSave:
},
}
def save_images_meta(self, images, add_date_to_filename, add_time_to_filename, add_seed_to_filename, add_size_to_filename, save_mata_to_json, save_info_to_txt, image_metadata=None,
def save_images_meta(self, images, add_date_to_filename, add_time_to_filename, add_seed_to_filename, add_size_to_filename, save_meta_to_json, save_info_to_txt, image_metadata=None,
output_path='[time(%Y-%m-%d)]', subpath='Project', add_modelname_to_path = False, filename_prefix="ComfyUI", filename_delimiter='_',
extension='jpg', quality=95, prompt=None, extra_pnginfo=None,
overwrite_mode='false', filename_number_padding=2, filename_number_start=False,
png_embed_workflow=False, image_embed_exif=False, save_image=True):
png_embed_workflow=False, png_embed_data=False, image_embed_exif=False, save_image=True):
if save_image == False:
saved_info = "*** Image saver switched OFF, image not saved. ***"
@@ -209,6 +210,8 @@ class PrimereMetaSave:
exif_metadata_json = image_metadata
if extension == 'png':
if png_embed_data == True:
metadata.add_text("gendata", json.dumps(exif_metadata_json))
img.save(output_file, pnginfo=metadata, optimize=True)
elif extension == 'webp':
img.save(output_file, quality=quality, exif=metadata)
@@ -227,7 +230,7 @@ class PrimereMetaSave:
img.save(output_file, quality=quality, optimize=True)
print(f"Image file saved without exif: {output_file}")
if save_mata_to_json:
if save_meta_to_json:
jsonfile = os.path.splitext(output_file)[0] + '.json'
with open(jsonfile, 'w', encoding='utf-8') as jf:
json.dump(exif_metadata_json, jf, ensure_ascii=False, indent=4)
+72 -76
View File
@@ -29,101 +29,97 @@ class ComfyUI(BaseFormat):
def _comfy_png(self):
prompt = self._info.get("prompt") or {}
gendata = self._info.get("gendata") or {}
gendata_json = json.loads(gendata)
workflow = self._info.get("workflow") or {}
prompt_json = json.loads(prompt)
# find end node of each flow
end_nodes = list(filter( lambda item: item[-1].get("class_type") in ["SaveImage"] + KSAMPLER_TYPES, prompt_json.items(),))
longest_flow = {}
longest_nodes = []
longest_flow_len = 0
if len(gendata_json) > 0:
FINAL_DICT = gendata_json
self._parameter = FINAL_DICT
else:
# find end node of each flow
end_nodes = list(filter( lambda item: item[-1].get("class_type") in ["SaveImage"] + KSAMPLER_TYPES, prompt_json.items(),))
longest_flow = {}
longest_nodes = []
longest_flow_len = 0
for end_node in end_nodes:
flow, nodes = self._comfy_traverse(prompt_json, str(end_node[0]))
if len(nodes) > longest_flow_len:
longest_flow = flow
longest_nodes = nodes
longest_flow_len = len(nodes)
for end_node in end_nodes:
flow, nodes = self._comfy_traverse(prompt_json, str(end_node[0]))
if len(nodes) > longest_flow_len:
longest_flow = flow
longest_nodes = nodes
longest_flow_len = len(nodes)
SizeID = None
ModelID = None
PositiveID = None
NegativeID = None
try:
if 'latent_image' in flow:
SizeID = flow['latent_image'][0]
except Exception:
SizeID = None
try:
if 'model' in flow:
ModelID = flow['model'][0]
except Exception:
ModelID = None
try:
if 'positive' in flow:
PositiveID = flow['positive'][0]
except Exception:
PositiveID = None
try:
if 'negative' in flow:
NegativeID = flow['negative'][0]
except Exception:
NegativeID = None
FINAL_DICT = {}
FINAL_DICT['negative'] = ""
FINAL_DICT['positive'] = ""
try:
if 'latent_image' in flow:
SizeID = flow['latent_image'][0]
except Exception:
SizeID = None
if PositiveID == None:
PositiveID = '1'
if NegativeID == None:
NegativeID = '1'
try:
if 'model' in flow:
ModelID = flow['model'][0]
except Exception:
ModelID = None
if PositiveID and NegativeID and 'text_g' in prompt_json[PositiveID]['inputs']:
FINAL_DICT['positive'] = prompt_json[PositiveID]['inputs']['text_g']
FINAL_DICT['negative'] = prompt_json[NegativeID]['inputs']['text_g']
try:
if 'positive' in flow:
PositiveID = flow['positive'][0]
except Exception:
PositiveID = None
if PositiveID and NegativeID and 'text' in prompt_json[PositiveID]['inputs']:
FINAL_DICT['positive'] = prompt_json[PositiveID]['inputs']['text']
FINAL_DICT['negative'] = prompt_json[NegativeID]['inputs']['text']
try:
if 'negative' in flow:
NegativeID = flow['negative'][0]
except Exception:
NegativeID = None
if PositiveID and 'positive_prompt' in prompt_json[PositiveID]['inputs']:
FINAL_DICT['positive'] = prompt_json[PositiveID]['inputs']['positive_prompt']
if NegativeID and 'negative_prompt' in prompt_json[PositiveID]['inputs']:
FINAL_DICT['negative'] = prompt_json[NegativeID]['inputs']['negative_prompt']
FINAL_DICT = {}
FINAL_DICT['negative'] = ""
FINAL_DICT['positive'] = ""
if PositiveID == None or ('text_g' not in prompt_json[PositiveID]['inputs'] and 'text' not in prompt_json[PositiveID]['inputs'] and 'positive_prompt' not in prompt_json[PositiveID]['inputs']):
if hasattr(self, '_positive'):
FINAL_DICT['positive'] = self._positive
if hasattr(self, '_negative'):
FINAL_DICT['negative'] = self._negative
if PositiveID and NegativeID and 'text_g' in prompt_json[PositiveID]['inputs']:
FINAL_DICT['positive'] = prompt_json[PositiveID]['inputs']['text_g']
FINAL_DICT['negative'] = prompt_json[NegativeID]['inputs']['text_g']
if 'steps' in flow and type(flow['steps']) == int:
FINAL_DICT['steps'] = flow['steps']
if 'sampler_name' in flow and 'scheduler' in flow and type(flow['sampler_name']) == str and type(flow['scheduler']) == str:
FINAL_DICT['sampler'] = flow['sampler_name'] + ' ' + flow['scheduler']
if 'seed' in flow and type(flow['seed']) == int:
FINAL_DICT['seed'] = flow['seed']
if 'cfg' in flow and (type(flow['cfg']) == int or type(flow['cfg']) == float):
FINAL_DICT['cfg_scale'] = flow['cfg']
if PositiveID and NegativeID and 'text' in prompt_json[PositiveID]['inputs']:
FINAL_DICT['positive'] = prompt_json[PositiveID]['inputs']['text']
FINAL_DICT['negative'] = prompt_json[NegativeID]['inputs']['text']
if ModelID and 'ckpt_name' in prompt_json[ModelID]['inputs'] and type(prompt_json[ModelID]['inputs']['ckpt_name']) == str:
FINAL_DICT['model_name'] = prompt_json[ModelID]['inputs']['ckpt_name'] # flow['ckpt_name']
elif 'ckpt_name' in flow and type(flow['ckpt_name']) == str:
FINAL_DICT['model_name'] = flow['ckpt_name']
if PositiveID == None or ('text_g' not in prompt_json[PositiveID]['inputs'] and 'text' not in prompt_json[PositiveID]['inputs']):
if hasattr(self, '_positive'):
FINAL_DICT['positive'] = self._positive
if hasattr(self, '_negative'):
FINAL_DICT['negative'] = self._negative
if SizeID and 'width' in prompt_json[SizeID]['inputs'] and 'height' in prompt_json[SizeID]['inputs'] and type(prompt_json[SizeID]['inputs']['width']) == int:
origwidth = str(prompt_json[SizeID]['inputs']['width'])
origheight = str(prompt_json[SizeID]['inputs']['height'])
FINAL_DICT['width'] = int(origwidth)
FINAL_DICT['height'] = int(origheight)
FINAL_DICT['size_string'] = origwidth + 'x' + origheight
if 'steps' in flow and type(flow['steps']) == int:
FINAL_DICT['steps'] = flow['steps']
if 'sampler_name' in flow and 'scheduler' in flow and type(flow['sampler_name']) == str and type(flow['scheduler']) == str:
FINAL_DICT['sampler'] = flow['sampler_name'] + ' ' + flow['scheduler']
if 'seed' in flow and type(flow['seed']) == int:
FINAL_DICT['seed'] = flow['seed']
if 'cfg' in flow and (type(flow['cfg']) == int or type(flow['cfg']) == float):
FINAL_DICT['cfg_scale'] = flow['cfg']
self._parameter = FINAL_DICT
if ModelID and 'ckpt_name' in prompt_json[ModelID]['inputs'] and type(prompt_json[ModelID]['inputs']['ckpt_name']) == str:
FINAL_DICT['model_name'] = prompt_json[ModelID]['inputs']['ckpt_name'] # flow['ckpt_name']
elif 'ckpt_name' in flow and type(flow['ckpt_name']) == str:
FINAL_DICT['model_name'] = flow['ckpt_name']
if SizeID and 'width' in prompt_json[SizeID]['inputs'] and 'height' in prompt_json[SizeID]['inputs'] and type(prompt_json[SizeID]['inputs']['width']) == int:
origwidth = str(prompt_json[SizeID]['inputs']['width'])
origheight = str(prompt_json[SizeID]['inputs']['height'])
FINAL_DICT['width'] = int(origwidth)
FINAL_DICT['height'] = int(origheight)
FINAL_DICT['size_string'] = origwidth + 'x' + origheight
self._parameter = FINAL_DICT
def _comfy_traverse(self, prompt, end_node):
flow = {}