noumanjavaid commited on
Commit
39dd6f5
·
verified ·
1 Parent(s): db1515c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -17
app.py CHANGED
@@ -5,9 +5,9 @@ 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))
@@ -19,6 +19,26 @@ def highlight_watermark(image, coords=(0, 0, 100, 50), color="red", width=5):
19
  print(f"Error highlighting: {e}")
20
  return image
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def choose_encode(inp_im, inp_mark, cho):
23
  try:
24
  if not inp_im:
@@ -31,23 +51,19 @@ def choose_encode(inp_im, inp_mark, cho):
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)
@@ -58,13 +74,12 @@ def choose_encode(inp_im, inp_mark, cho):
58
  img = Image.open(out_im_path)
59
  coords = utils.png_encode_coords if hasattr(utils, 'png_encode_coords') else (50, 50, 200, 50)
60
  highlighted_image = highlight_watermark(img.copy(), coords)
61
-
62
- img_byte_arr = io.BytesIO()
63
- highlighted_image.save(img_byte_arr, format='PNG')
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
 
@@ -111,4 +126,4 @@ with gr.Blocks() as app:
111
  det_btn.click(detect_watermark, inputs=[det_im], outputs=[det_msg])
112
 
113
  if __name__ == "__main__":
114
- app.launch()
 
5
  import os
6
  import cv2
7
  import numpy as np
8
+ import tempfile
9
 
10
  def highlight_watermark(image, coords=(0, 0, 100, 50), color="red", width=5):
 
11
  try:
12
  if isinstance(image, np.ndarray):
13
  image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
 
19
  print(f"Error highlighting: {e}")
20
  return image
21
 
22
+ def create_output_file(image, original_filename):
23
+ try:
24
+ # Create temp directory if it doesn't exist
25
+ temp_dir = "temp"
26
+ os.makedirs(temp_dir, exist_ok=True)
27
+
28
+ # Generate output filename
29
+ base_name = os.path.splitext(os.path.basename(original_filename))[0]
30
+ output_path = os.path.join(temp_dir, f"{base_name}_watermarked.png")
31
+
32
+ # Save image
33
+ if isinstance(image, np.ndarray):
34
+ image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
35
+ image.save(output_path, format='PNG')
36
+
37
+ return output_path
38
+ except Exception as e:
39
+ print(f"Error creating output file: {e}")
40
+ return None
41
+
42
  def choose_encode(inp_im, inp_mark, cho):
43
  try:
44
  if not inp_im:
 
51
  return None, "Invalid image format. Use PNG or JPG.", None
52
 
53
  if cho == "stegan":
 
54
  watermarked_image, error_msg = utils.encode(inp_im, inp_mark)
55
 
56
  if error_msg:
57
  return None, error_msg, None
58
 
 
59
  image_pil = Image.fromarray(cv2.cvtColor(watermarked_image, cv2.COLOR_BGR2RGB))
60
  highlighted_image = highlight_watermark(image_pil.copy(), (0, 0, image_pil.width, image_pil.height), color="blue")
61
 
62
+ output_path = create_output_file(highlighted_image, inp_im)
63
+ if not output_path:
64
+ return None, "Error saving watermarked image.", None
 
 
65
 
66
+ return highlighted_image, "Steganography watermark added successfully.", output_path
67
 
68
  elif cho == "pnginfo":
69
  out_im_path, error_msg = utils.png_encode(inp_im, inp_mark)
 
74
  img = Image.open(out_im_path)
75
  coords = utils.png_encode_coords if hasattr(utils, 'png_encode_coords') else (50, 50, 200, 50)
76
  highlighted_image = highlight_watermark(img.copy(), coords)
77
+
78
+ output_path = create_output_file(highlighted_image, inp_im)
79
+ if not output_path:
80
+ return None, "Error saving watermarked image.", None
81
+
82
+ return highlighted_image, "PNG metadata watermark added successfully.", output_path
 
83
  except Exception as e:
84
  return None, f"Error processing PNG: {e}", None
85
 
 
126
  det_btn.click(detect_watermark, inputs=[det_im], outputs=[det_msg])
127
 
128
  if __name__ == "__main__":
129
+ app.launch()