Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,110 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import utils
|
|
|
|
|
|
|
3 |
|
4 |
-
def
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
with gr.Blocks() as app:
|
13 |
with gr.Tab("Add Watermark"):
|
14 |
-
cho = gr.Radio(choices=["stegan","pnginfo"],value="stegan")
|
15 |
with gr.Row():
|
16 |
with gr.Column():
|
17 |
-
inp_im = gr.Image(label="Input Image",type="filepath")
|
18 |
inp_mark = gr.Textbox(label="Watermark")
|
19 |
-
mark_btn = gr.Button()
|
20 |
msg_box = gr.Textbox(label="System Message")
|
21 |
with gr.Column():
|
22 |
out_im = gr.Image(label="Watermarked Image")
|
|
|
23 |
with gr.Tab("Detect Watermark"):
|
24 |
with gr.Row():
|
25 |
with gr.Column():
|
26 |
-
det_im = gr.Image(label="Watermarked Image",type="filepath")
|
27 |
det_btn = gr.Button("Detect")
|
28 |
with gr.Column():
|
29 |
-
det_msg = gr.Textbox(label="Detected Watermark",lines=6, max_lines=50)
|
30 |
-
|
31 |
-
|
|
|
|
|
32 |
app.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image, ImageDraw
|
3 |
import utils
|
4 |
+
import io
|
5 |
+
import os
|
6 |
+
import cv2
|
7 |
|
8 |
+
def highlight_watermark(image, coords=(0, 0, 100, 50), color="red", width=5):
|
9 |
+
"""Highlights watermark area. Adapts rectangle size based on coordinates."""
|
10 |
+
try:
|
11 |
+
draw = ImageDraw.Draw(image)
|
12 |
+
x, y, w, h = coords
|
13 |
+
draw.rectangle((max(0, x - 5), max(0, y - 5), x + w + 5, y + h + 5), outline=color, width=width)
|
14 |
+
return image
|
15 |
+
except Exception as e:
|
16 |
+
print(f"Error highlighting: {e}")
|
17 |
+
return image
|
18 |
+
|
19 |
+
def choose_encode(inp_im, inp_mark, cho):
|
20 |
+
try:
|
21 |
+
if not inp_im:
|
22 |
+
return None, "Please upload an image.", None
|
23 |
+
if not inp_mark:
|
24 |
+
return None, "Please enter watermark text.", None
|
25 |
+
|
26 |
+
valid_image_types = ["png", "jpg", "jpeg", "gif", "bmp", "tiff", "webp"]
|
27 |
+
if not any(inp_im.lower().endswith("." + ext) for ext in valid_image_types):
|
28 |
+
return None, "Invalid image format. Use PNG or JPG.", None
|
29 |
+
|
30 |
+
if cho == "stegan":
|
31 |
+
image_pil = Image.open(inp_im)
|
32 |
+
image_cv2, msg = utils.encode(inp_im, inp_mark)
|
33 |
+
if msg:
|
34 |
+
return None, msg, None
|
35 |
+
|
36 |
+
image_pil = Image.fromarray(cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB))
|
37 |
+
highlighted_image = highlight_watermark(image_pil.copy(), (0, 0, image_pil.width, image_pil.height), color="blue")
|
38 |
+
|
39 |
+
img_byte_arr = io.BytesIO()
|
40 |
+
highlighted_image.save(img_byte_arr, format='PNG')
|
41 |
+
img_byte_arr.seek(0)
|
42 |
+
download_name = os.path.splitext(os.path.basename(inp_im))[0] + "_watermarked.png"
|
43 |
+
return highlighted_image, "Steganography watermark added successfully.", (download_name, img_byte_arr.read())
|
44 |
+
|
45 |
+
elif cho == "pnginfo":
|
46 |
+
out_im_path, out_msg = utils.png_encode(inp_im, inp_mark)
|
47 |
+
if out_msg: # Handle potential errors from png_encode
|
48 |
+
return None, out_msg, None
|
49 |
+
try:
|
50 |
+
img = Image.open(out_im_path)
|
51 |
+
coords = utils.png_encode_coords if hasattr(utils, 'png_encode_coords') else (50, 50, 200, 50)
|
52 |
+
highlighted_image = highlight_watermark(img.copy(), coords)
|
53 |
+
|
54 |
+
img_byte_arr = io.BytesIO()
|
55 |
+
highlighted_image.save(img_byte_arr, format='PNG')
|
56 |
+
img_byte_arr.seek(0)
|
57 |
+
download_name = os.path.splitext(os.path.basename(inp_im))[0] + "_watermarked.png"
|
58 |
+
|
59 |
+
return highlighted_image, "PNG metadata watermark added successfully.", (download_name, img_byte_arr.read())
|
60 |
+
except Exception as e:
|
61 |
+
return None, f"Error processing PNG: {e}", None
|
62 |
+
|
63 |
+
|
64 |
+
except FileNotFoundError:
|
65 |
+
return None, "Image file not found.", None
|
66 |
+
except ValueError as e:
|
67 |
+
return None, f"Invalid input: {e}", None
|
68 |
+
except Exception as e:
|
69 |
+
return None, f"An unexpected error occurred: {e}", None
|
70 |
+
|
71 |
+
|
72 |
+
def detect_watermark(det_im):
|
73 |
+
if not det_im:
|
74 |
+
return "Please upload an image."
|
75 |
+
|
76 |
+
valid_image_types = ["png", "jpg", "jpeg", "gif", "bmp", "tiff", "webp"]
|
77 |
+
if not any(det_im.lower().endswith("." + ext) for ext in valid_image_types):
|
78 |
+
return "Invalid image format. Use PNG or JPG."
|
79 |
+
|
80 |
+
det_msg = utils.decode(det_im)
|
81 |
+
|
82 |
+
if det_msg is None or det_msg == "":
|
83 |
+
return "No watermark detected or not encoded with this tool."
|
84 |
+
|
85 |
+
return det_msg
|
86 |
|
87 |
with gr.Blocks() as app:
|
88 |
with gr.Tab("Add Watermark"):
|
89 |
+
cho = gr.Radio(choices=["stegan", "pnginfo"], value="stegan")
|
90 |
with gr.Row():
|
91 |
with gr.Column():
|
92 |
+
inp_im = gr.Image(label="Input Image", type="filepath")
|
93 |
inp_mark = gr.Textbox(label="Watermark")
|
94 |
+
mark_btn = gr.Button("Add Watermark")
|
95 |
msg_box = gr.Textbox(label="System Message")
|
96 |
with gr.Column():
|
97 |
out_im = gr.Image(label="Watermarked Image")
|
98 |
+
file_output = gr.File()
|
99 |
with gr.Tab("Detect Watermark"):
|
100 |
with gr.Row():
|
101 |
with gr.Column():
|
102 |
+
det_im = gr.Image(label="Watermarked Image", type="filepath")
|
103 |
det_btn = gr.Button("Detect")
|
104 |
with gr.Column():
|
105 |
+
det_msg = gr.Textbox(label="Detected Watermark", lines=6, max_lines=50)
|
106 |
+
|
107 |
+
mark_btn.click(choose_encode, [inp_im, inp_mark, cho], [out_im, msg_box, file_output])
|
108 |
+
det_btn.click(utils.decode, [det_im], det_msg)
|
109 |
+
|
110 |
app.launch()
|