noumanjavaid commited on
Commit
e7ccac1
·
verified ·
1 Parent(s): 26f3597

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -7,6 +7,7 @@ import cv2
7
  import numpy as np
8
 
9
  def highlight_watermark(image, coords=(0, 0, 100, 50), color="red", width=5):
 
10
  try:
11
  if isinstance(image, np.ndarray):
12
  image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
@@ -30,25 +31,29 @@ def choose_encode(inp_im, inp_mark, cho):
30
  return None, "Invalid image format. Use PNG or JPG.", None
31
 
32
  if cho == "stegan":
 
33
  watermarked_image, error_msg = utils.encode(inp_im, inp_mark)
 
34
  if error_msg:
35
  return None, error_msg, None
36
-
 
37
  image_pil = Image.fromarray(cv2.cvtColor(watermarked_image, cv2.COLOR_BGR2RGB))
38
  highlighted_image = highlight_watermark(image_pil.copy(), (0, 0, image_pil.width, image_pil.height), color="blue")
39
 
 
40
  img_byte_arr = io.BytesIO()
41
  highlighted_image.save(img_byte_arr, format='PNG')
42
  img_byte_arr.seek(0)
43
  download_name = os.path.splitext(os.path.basename(inp_im))[0] + "_watermarked.png"
44
 
45
- return highlighted_image, "Steganography watermark added successfully.", (download_name, img_byte_arr.getvalue())
46
 
47
  elif cho == "pnginfo":
48
  out_im_path, error_msg = utils.png_encode(inp_im, inp_mark)
49
  if error_msg:
50
  return None, error_msg, None
51
-
52
  try:
53
  img = Image.open(out_im_path)
54
  coords = utils.png_encode_coords if hasattr(utils, 'png_encode_coords') else (50, 50, 200, 50)
@@ -59,7 +64,7 @@ def choose_encode(inp_im, inp_mark, cho):
59
  img_byte_arr.seek(0)
60
  download_name = os.path.splitext(os.path.basename(inp_im))[0] + "_watermarked.png"
61
 
62
- return highlighted_image, "PNG metadata watermark added successfully.", (download_name, img_byte_arr.getvalue())
63
  except Exception as e:
64
  return None, f"Error processing PNG: {e}", None
65
 
@@ -80,6 +85,7 @@ def detect_watermark(det_im):
80
 
81
  return detected_text
82
 
 
83
  with gr.Blocks() as app:
84
  with gr.Tab("Add Watermark"):
85
  cho = gr.Radio(choices=["stegan", "pnginfo"], value="stegan", label="Watermark Method")
@@ -105,4 +111,4 @@ with gr.Blocks() as app:
105
  det_btn.click(detect_watermark, inputs=[det_im], outputs=[det_msg])
106
 
107
  if __name__ == "__main__":
108
- app.launch()
 
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))
 
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)
 
64
  img_byte_arr.seek(0)
65
  download_name = os.path.splitext(os.path.basename(inp_im))[0] + "_watermarked.png"
66
 
67
+ return highlighted_image, "PNG metadata watermark added successfully.", (download_name, img_byte_arr.read())
68
  except Exception as e:
69
  return None, f"Error processing PNG: {e}", None
70
 
 
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")
 
111
  det_btn.click(detect_watermark, inputs=[det_im], outputs=[det_msg])
112
 
113
  if __name__ == "__main__":
114
+ app.launch()