VOIDER commited on
Commit
821a65f
·
verified ·
1 Parent(s): 3c2ef44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -33
app.py CHANGED
@@ -1,52 +1,46 @@
1
  import gradio as gr
2
- from PIL import Image
3
  import io
4
- import re
5
 
6
  def extract_metadata(image):
7
  """Extracts Stable Diffusion metadata from a PIL Image."""
8
  if image:
9
- metadata = image.text.get("parameters", "")
 
10
  else:
11
  metadata = ""
12
  return image, metadata
13
 
14
-
15
  def update_metadata(image, new_metadata):
16
- """Updates the Stable Diffusion metadata in a PIL Image and saves it."""
17
  if image:
18
- # Convert PIL Image to bytes
19
- img_byte_arr = io.BytesIO()
20
- image.save(img_byte_arr, format='PNG', pnginfo=image.text)
21
- img_byte_arr = img_byte_arr.getvalue()
22
-
23
- # Reload image to apply changes. PIL is a bit finicky about modifying .text
24
- img = Image.open(io.BytesIO(img_byte_arr))
25
 
26
- img.text["parameters"] = new_metadata
27
-
28
- # Save to a BytesIO object
29
  output_bytes = io.BytesIO()
30
- img.save(output_bytes, format="PNG") # Save back as PNG with updated metadata
31
- output_bytes.seek(0) # Important: Rewind the buffer to the beginning
 
 
 
32
 
33
- return output_bytes, new_metadata
34
  else:
35
- return None, ""
36
 
37
  def clear_metadata(image, metadata):
38
- """Clears the Stable Diffusion Metadata"""
39
  return update_metadata(image, "")
40
 
41
 
42
  def copy_metadata(metadata: str):
43
- """Copies the metadata to the clipboard (simulated)."""
44
- # gradio.Info("Metadata copied to clipboard!") # Informational message
45
- metadata = metadata.strip() # Clean up leading/trailing whitespace
46
- # Clears Used Embeddings
47
  embed_index = metadata.find("\nUsed embeddings: ")
48
  if embed_index < 0:
49
- embed_index = metadata.find("\nUsed embeddings: ")
50
  if embed_index > 0:
51
  metadata = metadata[:embed_index]
52
 
@@ -57,7 +51,6 @@ def process_image(image):
57
  return extract_metadata(image)
58
 
59
 
60
-
61
  with gr.Blocks() as demo:
62
  gr.Markdown("# SD EXIF Editor (Gradio)")
63
  gr.Markdown("Upload a PNG image generated by Stable Diffusion WebUI (AUTOMATIC1111) to view and edit its metadata.")
@@ -65,8 +58,7 @@ with gr.Blocks() as demo:
65
  with gr.Row():
66
  with gr.Column():
67
  image_input = gr.Image(type="pil", label="Upload PNG Image")
68
- image_output = gr.Image(type="pil", label="Processed Image")
69
- # image_output = gr.Image(type="filepath", label="Processed Image") #For local testing.
70
 
71
  with gr.Column():
72
  metadata_textbox = gr.Textbox(label="Metadata", lines=5)
@@ -75,12 +67,10 @@ with gr.Blocks() as demo:
75
  clear_button = gr.Button("Clear Metadata")
76
  save_button = gr.Button("Save Metadata")
77
 
78
-
79
  # --- Event Handling ---
80
  image_input.change(process_image, inputs=[image_input], outputs=[image_output, metadata_textbox])
81
- save_button.click(update_metadata, inputs=[image_output, metadata_textbox], outputs=[image_output, metadata_textbox])
82
- clear_button.click(clear_metadata, inputs=[image_output, metadata_textbox], outputs=[image_output, metadata_textbox])
83
  copy_button.click(copy_metadata, inputs=[metadata_textbox], outputs=[metadata_textbox])
84
 
85
-
86
- demo.launch()
 
1
  import gradio as gr
2
+ from PIL import Image, PngImagePlugin
3
  import io
 
4
 
5
  def extract_metadata(image):
6
  """Extracts Stable Diffusion metadata from a PIL Image."""
7
  if image:
8
+ # Access metadata using the 'info' dictionary
9
+ metadata = image.info.get("parameters", "")
10
  else:
11
  metadata = ""
12
  return image, metadata
13
 
 
14
  def update_metadata(image, new_metadata):
15
+ """Updates the Stable Diffusion metadata in a PIL Image."""
16
  if image:
17
+ # Create a PngInfo object to store metadata
18
+ pnginfo = PngImagePlugin.PngInfo()
19
+ pnginfo.add_text("parameters", new_metadata)
 
 
 
 
20
 
21
+ # Save the image to a BytesIO object with the updated metadata
 
 
22
  output_bytes = io.BytesIO()
23
+ image.save(output_bytes, format="PNG", pnginfo=pnginfo)
24
+ output_bytes.seek(0)
25
+
26
+ # Re-open the image from the BytesIO object to ensure Gradio displays the updated image
27
+ updated_image = Image.open(output_bytes)
28
 
29
+ return updated_image, new_metadata # Return updated PIL Image
30
  else:
31
+ return None, ""
32
 
33
  def clear_metadata(image, metadata):
34
+ """Clears the Stable Diffusion Metadata."""
35
  return update_metadata(image, "")
36
 
37
 
38
  def copy_metadata(metadata: str):
39
+ """Copies the metadata to the clipboard (simulated in Gradio)."""
40
+ metadata = metadata.strip()
 
 
41
  embed_index = metadata.find("\nUsed embeddings: ")
42
  if embed_index < 0:
43
+ embed_index = metadata.find("\nUsed embeddings: ") # Corrected typo
44
  if embed_index > 0:
45
  metadata = metadata[:embed_index]
46
 
 
51
  return extract_metadata(image)
52
 
53
 
 
54
  with gr.Blocks() as demo:
55
  gr.Markdown("# SD EXIF Editor (Gradio)")
56
  gr.Markdown("Upload a PNG image generated by Stable Diffusion WebUI (AUTOMATIC1111) to view and edit its metadata.")
 
58
  with gr.Row():
59
  with gr.Column():
60
  image_input = gr.Image(type="pil", label="Upload PNG Image")
61
+ image_output = gr.Image(type="pil", label="Processed Image") # Keep as PIL
 
62
 
63
  with gr.Column():
64
  metadata_textbox = gr.Textbox(label="Metadata", lines=5)
 
67
  clear_button = gr.Button("Clear Metadata")
68
  save_button = gr.Button("Save Metadata")
69
 
 
70
  # --- Event Handling ---
71
  image_input.change(process_image, inputs=[image_input], outputs=[image_output, metadata_textbox])
72
+ save_button.click(update_metadata, inputs=[image_input, metadata_textbox], outputs=[image_output, metadata_textbox]) # Use image_input
73
+ clear_button.click(clear_metadata, inputs=[image_input, metadata_textbox], outputs=[image_output, metadata_textbox]) # Use image_input
74
  copy_button.click(copy_metadata, inputs=[metadata_textbox], outputs=[metadata_textbox])
75
 
76
+ demo.launch(share=True) # Enable public link