Threadbourne commited on
Commit
909b69b
·
verified ·
1 Parent(s): 5041796

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import gradio as gr
3
+ import io, os, tempfile
4
+
5
+ def _get_path(uploaded):
6
+ if uploaded is None:
7
+ return None
8
+ if isinstance(uploaded, dict):
9
+ return uploaded.get("name")
10
+ try:
11
+ return uploaded.name
12
+ except Exception:
13
+ return uploaded
14
+
15
+ def process(uploaded, keep_metadata):
16
+ """uploaded: gr.File input; keep_metadata: checkbox (opt-in)
17
+ Returns: preview (PIL.Image), downloadable file path, status message
18
+ """
19
+ path = _get_path(uploaded)
20
+ if not path:
21
+ return None, None, "No file uploaded."
22
+ try:
23
+ img = Image.open(path)
24
+ except Exception as e:
25
+ return None, None, f"Cannot open image: {e}"
26
+
27
+ fmt = (img.format or "PNG").upper()
28
+
29
+ if keep_metadata:
30
+ # Opt-in path: return original bytes so metadata is preserved
31
+ with open(path, "rb") as f:
32
+ data = f.read()
33
+ preview = img.convert("RGB")
34
+ tmp = tempfile.NamedTemporaryFile(delete=False, suffix='.' + fmt.lower())
35
+ tmp.write(data); tmp.flush(); tmp.close()
36
+ return preview, tmp.name, "Original metadata preserved (opt‑in)."
37
+
38
+ # Privacy path: re-save into a fresh buffer (no exif passed)
39
+ preview_img = img.convert("RGB")
40
+ buf = io.BytesIO()
41
+ preview_img.save(buf, format=fmt)
42
+ buf.seek(0)
43
+
44
+ # Verify removal: try getexif(); fallback to info['exif']
45
+ try:
46
+ img2 = Image.open(buf)
47
+ ex = img2.getexif()
48
+ has_exif = bool(len(ex))
49
+ except Exception:
50
+ has_exif = bool(img2.info.get("exif")) if hasattr(img2, 'info') else False
51
+
52
+ ok = not has_exif
53
+ msg = "Metadata removed ✓" if ok else "Metadata may remain (verification failed)."
54
+
55
+ # write sanitized file for download
56
+ out_tmp = tempfile.NamedTemporaryFile(delete=False, suffix='.' + fmt.lower())
57
+ out_tmp.write(buf.getvalue()); out_tmp.flush(); out_tmp.close()
58
+
59
+ return preview_img, out_tmp.name, msg
60
+
61
+ with gr.Blocks() as demo:
62
+ gr.Markdown("**Upload an image — metadata will be stripped unless you opt into keeping it.**")
63
+ file_in = gr.File(label='Upload image (PNG/JPEG...)')
64
+ keep = gr.Checkbox(label='Keep original metadata (opt‑in)', value=False)
65
+ img_out = gr.Image(label='Processed preview')
66
+ file_out = gr.File(label='Download processed file')
67
+ status = gr.Textbox(label='Status')
68
+ btn = gr.Button('Process')
69
+ btn.click(process, inputs=[file_in, keep], outputs=[img_out, file_out, status])
70
+ demo.launch()