Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from PIL import Image, PngImagePlugin
|
| 4 |
+
|
| 5 |
+
# --- Helper functions ---
|
| 6 |
+
|
| 7 |
+
def load_image(file_obj):
|
| 8 |
+
"""
|
| 9 |
+
Open the image and extract its 'parameters' metadata if available.
|
| 10 |
+
Only supports PNG images.
|
| 11 |
+
"""
|
| 12 |
+
if file_obj is None:
|
| 13 |
+
return None, ""
|
| 14 |
+
try:
|
| 15 |
+
img = Image.open(file_obj)
|
| 16 |
+
except Exception as e:
|
| 17 |
+
return None, f"Error loading image: {e}"
|
| 18 |
+
if img.format != "PNG":
|
| 19 |
+
return None, "Error: Only PNG images are supported."
|
| 20 |
+
# Extract the 'parameters' text if it exists
|
| 21 |
+
metadata = img.info.get("parameters", "")
|
| 22 |
+
return img, metadata
|
| 23 |
+
|
| 24 |
+
def update_png_metadata(img, new_metadata):
|
| 25 |
+
"""
|
| 26 |
+
Prepare PNG metadata: copy existing text chunks (except 'parameters')
|
| 27 |
+
and add/update the 'parameters' key.
|
| 28 |
+
"""
|
| 29 |
+
pnginfo = PngImagePlugin.PngInfo()
|
| 30 |
+
for key, value in img.info.items():
|
| 31 |
+
if key != "parameters":
|
| 32 |
+
pnginfo.add_text(key, value)
|
| 33 |
+
pnginfo.add_text("parameters", new_metadata)
|
| 34 |
+
return pnginfo
|
| 35 |
+
|
| 36 |
+
def save_image_with_metadata(img, new_metadata):
|
| 37 |
+
"""
|
| 38 |
+
Save the given image with updated metadata into a BytesIO stream,
|
| 39 |
+
then return the stream for download.
|
| 40 |
+
"""
|
| 41 |
+
output = io.BytesIO()
|
| 42 |
+
pnginfo = update_png_metadata(img, new_metadata)
|
| 43 |
+
# Save with updated PNG text chunks
|
| 44 |
+
img.save(output, format="PNG", pnginfo=pnginfo)
|
| 45 |
+
output.seek(0)
|
| 46 |
+
return output
|
| 47 |
+
|
| 48 |
+
def copy_metadata(metadata):
|
| 49 |
+
"""
|
| 50 |
+
Return a cleaned version of metadata by removing the 'Used embeddings:' part
|
| 51 |
+
(if present). This mimics the original behavior.
|
| 52 |
+
"""
|
| 53 |
+
# Look for a line starting with "Used embeddings:"
|
| 54 |
+
embed_index = metadata.find("\nUsed embeddings: ")
|
| 55 |
+
if embed_index < 0:
|
| 56 |
+
embed_index = metadata.find("\r\nUsed embeddings: ")
|
| 57 |
+
if embed_index > 0:
|
| 58 |
+
return metadata[:embed_index]
|
| 59 |
+
return metadata
|
| 60 |
+
|
| 61 |
+
# --- Gradio Interface functions ---
|
| 62 |
+
|
| 63 |
+
def load_image_wrapper(file_obj):
|
| 64 |
+
"""
|
| 65 |
+
Loads the image and returns:
|
| 66 |
+
- an image preview (PIL Image),
|
| 67 |
+
- the metadata text for editing,
|
| 68 |
+
- and the original image saved in state.
|
| 69 |
+
"""
|
| 70 |
+
img, meta = load_image(file_obj)
|
| 71 |
+
if img is None:
|
| 72 |
+
return None, meta, None
|
| 73 |
+
return img, meta, img
|
| 74 |
+
|
| 75 |
+
def update_image(img, metadata):
|
| 76 |
+
"""
|
| 77 |
+
Updates the image with new metadata and returns a file-like object for download.
|
| 78 |
+
"""
|
| 79 |
+
if img is None:
|
| 80 |
+
return None
|
| 81 |
+
updated_img = save_image_with_metadata(img, metadata)
|
| 82 |
+
return updated_img
|
| 83 |
+
|
| 84 |
+
def delete_metadata_wrapper(img):
|
| 85 |
+
"""
|
| 86 |
+
Clears the metadata (i.e. sets it to empty) and returns both an empty string
|
| 87 |
+
(to clear the textbox) and an updated image file.
|
| 88 |
+
"""
|
| 89 |
+
if img is None:
|
| 90 |
+
return "", None
|
| 91 |
+
updated_file = save_image_with_metadata(img, "")
|
| 92 |
+
return "", updated_file
|
| 93 |
+
|
| 94 |
+
# --- Build the Gradio Blocks interface ---
|
| 95 |
+
|
| 96 |
+
with gr.Blocks() as demo:
|
| 97 |
+
gr.Markdown("# SD EXIF Editor (Gradio Version)")
|
| 98 |
+
gr.Markdown("Upload a PNG image, view and edit the 'parameters' metadata, and download the updated image.")
|
| 99 |
+
|
| 100 |
+
with gr.Row():
|
| 101 |
+
with gr.Column():
|
| 102 |
+
image_input = gr.Image(label="Upload PNG Image", type="file")
|
| 103 |
+
load_btn = gr.Button("Load Image")
|
| 104 |
+
image_preview = gr.Image(label="Image Preview")
|
| 105 |
+
download_btn = gr.Button("Download Updated Image")
|
| 106 |
+
with gr.Column():
|
| 107 |
+
metadata_box = gr.Textbox(label="Metadata (parameters)", lines=10, placeholder="Metadata will appear here...")
|
| 108 |
+
copy_btn = gr.Button("Copy Clean Metadata")
|
| 109 |
+
delete_btn = gr.Button("Delete Metadata")
|
| 110 |
+
|
| 111 |
+
# A hidden state to store the PIL image object
|
| 112 |
+
image_state = gr.State(None)
|
| 113 |
+
|
| 114 |
+
# When the user uploads and clicks load, update preview, metadata, and state.
|
| 115 |
+
load_btn.click(load_image_wrapper, inputs=image_input, outputs=[image_preview, metadata_box, image_state])
|
| 116 |
+
|
| 117 |
+
# Download updated image (with new metadata) when the user clicks "Download Updated Image"
|
| 118 |
+
download_btn.click(update_image, inputs=[image_state, metadata_box], outputs=gr.File(label="Updated PNG Image"))
|
| 119 |
+
|
| 120 |
+
# When the user clicks "Copy Clean Metadata," remove the "Used embeddings:" part.
|
| 121 |
+
copy_btn.click(lambda meta: copy_metadata(meta), inputs=metadata_box, outputs=metadata_box)
|
| 122 |
+
|
| 123 |
+
# When the user clicks "Delete Metadata," clear the textbox and update the file.
|
| 124 |
+
delete_btn.click(delete_metadata_wrapper, inputs=image_state, outputs=[metadata_box, gr.File(label="Updated PNG Image")])
|
| 125 |
+
|
| 126 |
+
demo.launch()
|