noumanjavaid commited on
Commit
9b9ff61
·
verified ·
1 Parent(s): 1618e9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -28
app.py CHANGED
@@ -4,10 +4,13 @@ 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)
@@ -28,24 +31,29 @@ def choose_encode(inp_im, inp_mark, cho):
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)
@@ -60,15 +68,9 @@ def choose_encode(inp_im, inp_mark, cho):
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."
@@ -77,34 +79,36 @@ def detect_watermark(det_im):
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()
 
 
4
  import io
5
  import os
6
  import cv2
7
+ import numpy as np
8
 
9
  def highlight_watermark(image, coords=(0, 0, 100, 50), color="red", width=5):
10
  """Highlights watermark area. Adapts rectangle size based on coordinates."""
11
  try:
12
+ if isinstance(image, np.ndarray):
13
+ image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
14
  draw = ImageDraw.Draw(image)
15
  x, y, w, h = coords
16
  draw.rectangle((max(0, x - 5), max(0, y - 5), x + w + 5, y + h + 5), outline=color, width=width)
 
31
  return None, "Invalid image format. Use PNG or JPG.", None
32
 
33
  if cho == "stegan":
34
+ # Call encode function with the image path and watermark text
35
+ watermarked_image, error_msg = utils.encode(inp_im, inp_mark)
36
+
37
+ if error_msg:
38
+ return None, error_msg, None
39
+
40
+ # Convert to PIL Image and highlight
41
+ image_pil = Image.fromarray(cv2.cvtColor(watermarked_image, cv2.COLOR_BGR2RGB))
42
  highlighted_image = highlight_watermark(image_pil.copy(), (0, 0, image_pil.width, image_pil.height), color="blue")
43
+
44
+ # Prepare for download
45
  img_byte_arr = io.BytesIO()
46
  highlighted_image.save(img_byte_arr, format='PNG')
47
  img_byte_arr.seek(0)
48
  download_name = os.path.splitext(os.path.basename(inp_im))[0] + "_watermarked.png"
49
+
50
  return highlighted_image, "Steganography watermark added successfully.", (download_name, img_byte_arr.read())
51
 
52
  elif cho == "pnginfo":
53
+ out_im_path, error_msg = utils.png_encode(inp_im, inp_mark)
54
+ if error_msg:
55
+ return None, error_msg, None
56
+
57
  try:
58
  img = Image.open(out_im_path)
59
  coords = utils.png_encode_coords if hasattr(utils, 'png_encode_coords') else (50, 50, 200, 50)
 
68
  except Exception as e:
69
  return None, f"Error processing PNG: {e}", None
70
 
 
 
 
 
 
71
  except Exception as e:
72
  return None, f"An unexpected error occurred: {e}", None
73
 
 
74
  def detect_watermark(det_im):
75
  if not det_im:
76
  return "Please upload an image."
 
79
  if not any(det_im.lower().endswith("." + ext) for ext in valid_image_types):
80
  return "Invalid image format. Use PNG or JPG."
81
 
82
+ detected_text = utils.decode(det_im)
83
+ if not detected_text or detected_text.startswith("Error"):
 
84
  return "No watermark detected or not encoded with this tool."
85
 
86
+ return detected_text
87
 
88
+ # Create Gradio interface
89
  with gr.Blocks() as app:
90
  with gr.Tab("Add Watermark"):
91
+ cho = gr.Radio(choices=["stegan", "pnginfo"], value="stegan", label="Watermark Method")
92
  with gr.Row():
93
  with gr.Column():
94
  inp_im = gr.Image(label="Input Image", type="filepath")
95
+ inp_mark = gr.Textbox(label="Watermark Text")
96
  mark_btn = gr.Button("Add Watermark")
97
  msg_box = gr.Textbox(label="System Message")
98
  with gr.Column():
99
  out_im = gr.Image(label="Watermarked Image")
100
+ file_output = gr.File(label="Download Watermarked Image")
101
+
102
  with gr.Tab("Detect Watermark"):
103
  with gr.Row():
104
  with gr.Column():
105
  det_im = gr.Image(label="Watermarked Image", type="filepath")
106
+ det_btn = gr.Button("Detect Watermark")
107
  with gr.Column():
108
+ det_msg = gr.Textbox(label="Detected Watermark", lines=6)
109
 
110
+ mark_btn.click(choose_encode, inputs=[inp_im, inp_mark, cho], outputs=[out_im, msg_box, file_output])
111
+ det_btn.click(detect_watermark, inputs=[det_im], outputs=[det_msg])
112
 
113
+ if __name__ == "__main__":
114
+ app.launch()