From d174f71c593606deb3a6513ffe350373d620e198 Mon Sep 17 00:00:00 2001 From: pixxy Date: Thu, 17 Jul 2025 22:29:47 +0100 Subject: [PATCH 1/2] save metadata to images --- wgp.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/wgp.py b/wgp.py index bdb7327..37651b5 100644 --- a/wgp.py +++ b/wgp.py @@ -4738,11 +4738,15 @@ def generate_video( if metadata_choice == "json": with open(path.replace(f'.{extension}', '.json'), 'w') as f: json.dump(configs, f, indent=4) - elif metadata_choice == "metadata" and not is_image: - from mutagen.mp4 import MP4 - file = MP4(path) - file.tags['©cmt'] = [json.dumps(configs)] - file.save() + elif metadata_choice == "metadata": + if is_image: + with Image.open(path) as img: + img.save(path, comment=json.dumps(configs)) + else: + from mutagen.mp4 import MP4 + file = MP4(path) + file.tags['©cmt'] = [json.dumps(configs)] + file.save() if is_image: print(f"New image saved to Path: "+ path) else: @@ -7621,7 +7625,7 @@ def generate_configuration_tab(state, blocks, header, model_choice, prompt_enhan metadata_choice = gr.Dropdown( choices=[ ("Export JSON files", "json"), - ("Add metadata to video", "metadata"), + ("Embed metadata (Exif tag)", "metadata"), ("Neither", "none") ], value=server_config.get("metadata_type", "metadata"), From 378563691810cffb61ce050988f37e5d030cb997 Mon Sep 17 00:00:00 2001 From: pixxy Date: Fri, 18 Jul 2025 00:57:19 +0100 Subject: [PATCH 2/2] load metadata from images --- wgp.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/wgp.py b/wgp.py index 37651b5..21885ed 100644 --- a/wgp.py +++ b/wgp.py @@ -5452,7 +5452,7 @@ def apply_lset(state, wizard_prompt_activated, lset_name, loras_choices, loras_m return wizard_prompt_activated, loras_choices, loras_mult_choices, prompt, get_unique_id(), gr.update(), gr.update() else: - configs, any_video_file = get_settings_from_file(state, os.path.join(get_lora_dir(current_model_type), lset_name), True, True, True) + configs, _ = get_settings_from_file(state, os.path.join(get_lora_dir(current_model_type), lset_name), True, True, True) if configs == None: gr.Info("File not supported") return [gr.update()] * 7 @@ -5906,8 +5906,14 @@ def get_settings_from_file(state, file_path, allow_json, merge_with_defaults, sw tags = file.tags['©cmt'][0] except: pass - if tags != None: - configs = json.loads(tags) + elif file_path.endswith(".jpg"): + try: + with Image.open(file_path) as img: + tags = img.info["comment"] + except: + pass + if tags is not None: + configs = json.loads(tags) if configs == None: return None, False @@ -5942,7 +5948,7 @@ def load_settings_from_file(state, file_path): if file_path==None: return gr.update(), gr.update(), None - configs, any_video_file = get_settings_from_file(state, file_path, True, True, True) + configs, any_video_or_image_file = get_settings_from_file(state, file_path, True, True, True) if configs == None: gr.Info("File not supported") return gr.update(), gr.update(), None @@ -5950,9 +5956,10 @@ def load_settings_from_file(state, file_path): current_model_type = state["model_type"] model_type = configs["model_type"] prompt = configs.get("prompt", "") + is_image = configs.get("is_image", False) - if any_video_file: - gr.Info(f"Settings Loaded from Video generated with prompt '{prompt[:100]}'") + if any_video_or_image_file: + gr.Info(f"Settings Loaded from {'Image' if is_image else 'Video'} generated with prompt '{prompt[:100]}'") else: gr.Info(f"Settings Loaded from Settings file with prompt '{prompt[:100]}'") @@ -7140,7 +7147,7 @@ def generate_video_tab(update_form = False, state_dict = None, ui_defaults = Non save_settings_btn = gr.Button("Set Settings as Default", visible = not args.lock_config) export_settings_from_file_btn = gr.Button("Export Settings to File") with gr.Row(): - settings_file = gr.File(height=41,label="Load Settings From Video / Json") + settings_file = gr.File(height=41,label="Load Settings From Video / Image / JSON") settings_base64_output = gr.Text(interactive= False, visible=False, value = "") settings_filename = gr.Text(interactive= False, visible=False, value = "") @@ -8411,4 +8418,4 @@ if __name__ == "__main__": url = "http://" + server_name webbrowser.open(url + ":" + str(server_port), new = 0, autoraise = True) demo.launch(server_name=server_name, server_port=server_port, share=args.share, allowed_paths=[save_path]) -# Lucky me !!! \ No newline at end of file +# Lucky me !!!