Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -22,43 +22,44 @@ def encode(image_path, secret_data):
|
|
22 |
image = cv2.imread(image_path)
|
23 |
if image is None:
|
24 |
return None, "Failed to read image"
|
25 |
-
|
26 |
# Calculate maximum bytes for encoding
|
27 |
-
n_bytes = image.shape[0] * image.shape[1] * 3 // 8
|
28 |
|
29 |
-
#
|
30 |
secret_data_with_delimiter = f'{secret_data}#####'
|
31 |
if len(secret_data_with_delimiter) + 5 >= n_bytes:
|
32 |
return None, "Watermark is too large for Image Size"
|
33 |
-
|
34 |
-
# Add ending delimiter
|
35 |
-
secret_data_with_delimiter += "====="
|
36 |
|
37 |
-
|
38 |
binary_secret_data = to_bin(secret_data_with_delimiter)
|
39 |
data_len = len(binary_secret_data)
|
|
|
|
|
|
|
40 |
data_index = 0
|
41 |
|
42 |
-
# Encode the data
|
43 |
-
for i in range(
|
44 |
-
for j in range(
|
45 |
-
|
46 |
-
for color_channel in range(3):
|
47 |
if data_index < data_len:
|
48 |
-
# Get the
|
49 |
-
|
50 |
-
#
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
54 |
data_index += 1
|
55 |
-
|
|
|
56 |
if data_index >= data_len:
|
57 |
break
|
58 |
if data_index >= data_len:
|
59 |
break
|
60 |
-
|
61 |
-
return
|
62 |
except Exception as e:
|
63 |
return None, f"Error during encoding: {str(e)}"
|
64 |
|
@@ -68,23 +69,30 @@ def decode(image_path):
|
|
68 |
image = cv2.imread(image_path)
|
69 |
if image is None:
|
70 |
return "Failed to read image"
|
71 |
-
|
72 |
binary_data = ""
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
77 |
|
78 |
# Convert binary to text
|
79 |
-
all_bytes = [binary_data[i: i + 8] for i in range(0, len(binary_data), 8)]
|
80 |
decoded_data = ""
|
81 |
-
for
|
82 |
-
|
83 |
-
if
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
-
# Remove delimiters
|
87 |
-
return decoded_data[:-5].split("#####")[0]
|
88 |
except Exception as e:
|
89 |
return f"Error during decoding: {str(e)}"
|
90 |
|
@@ -109,4 +117,4 @@ def png_encode(im_name, extra):
|
|
109 |
return new_filename, None
|
110 |
|
111 |
except Exception as e:
|
112 |
-
return None, str(e)
|
|
|
22 |
image = cv2.imread(image_path)
|
23 |
if image is None:
|
24 |
return None, "Failed to read image"
|
25 |
+
|
26 |
# Calculate maximum bytes for encoding
|
27 |
+
n_bytes = (image.shape[0] * image.shape[1] * 3) // 8
|
28 |
|
29 |
+
# Prepare secret data
|
30 |
secret_data_with_delimiter = f'{secret_data}#####'
|
31 |
if len(secret_data_with_delimiter) + 5 >= n_bytes:
|
32 |
return None, "Watermark is too large for Image Size"
|
|
|
|
|
|
|
33 |
|
34 |
+
secret_data_with_delimiter += "====="
|
35 |
binary_secret_data = to_bin(secret_data_with_delimiter)
|
36 |
data_len = len(binary_secret_data)
|
37 |
+
|
38 |
+
# Create a copy of the image
|
39 |
+
watermarked_image = image.copy()
|
40 |
data_index = 0
|
41 |
|
42 |
+
# Encode the data
|
43 |
+
for i in range(watermarked_image.shape[0]):
|
44 |
+
for j in range(watermarked_image.shape[1]):
|
45 |
+
for k in range(3): # RGB channels
|
|
|
46 |
if data_index < data_len:
|
47 |
+
# Get the current pixel value
|
48 |
+
pixel = watermarked_image[i, j, k]
|
49 |
+
# Convert to binary and modify the least significant bit
|
50 |
+
binary_pixel = format(pixel, '08b')
|
51 |
+
new_pixel = binary_pixel[:-1] + binary_secret_data[data_index]
|
52 |
+
# Update the pixel value
|
53 |
+
watermarked_image[i, j, k] = int(new_pixel, 2)
|
54 |
data_index += 1
|
55 |
+
else:
|
56 |
+
break
|
57 |
if data_index >= data_len:
|
58 |
break
|
59 |
if data_index >= data_len:
|
60 |
break
|
61 |
+
|
62 |
+
return watermarked_image, None
|
63 |
except Exception as e:
|
64 |
return None, f"Error during encoding: {str(e)}"
|
65 |
|
|
|
69 |
image = cv2.imread(image_path)
|
70 |
if image is None:
|
71 |
return "Failed to read image"
|
72 |
+
|
73 |
binary_data = ""
|
74 |
+
# Extract the least significant bits
|
75 |
+
for i in range(image.shape[0]):
|
76 |
+
for j in range(image.shape[1]):
|
77 |
+
for k in range(3):
|
78 |
+
pixel = format(image[i, j, k], '08b')
|
79 |
+
binary_data += pixel[-1]
|
80 |
|
81 |
# Convert binary to text
|
|
|
82 |
decoded_data = ""
|
83 |
+
for i in range(0, len(binary_data), 8):
|
84 |
+
byte = binary_data[i:i+8]
|
85 |
+
if len(byte) == 8:
|
86 |
+
decoded_data += chr(int(byte, 2))
|
87 |
+
# Check for ending delimiter
|
88 |
+
if decoded_data[-5:] == "=====":
|
89 |
+
break
|
90 |
+
|
91 |
+
# Remove delimiters and return the message
|
92 |
+
if "#####" in decoded_data:
|
93 |
+
return decoded_data.split("#####")[0]
|
94 |
+
return "No watermark found"
|
95 |
|
|
|
|
|
96 |
except Exception as e:
|
97 |
return f"Error during decoding: {str(e)}"
|
98 |
|
|
|
117 |
return new_filename, None
|
118 |
|
119 |
except Exception as e:
|
120 |
+
return None, str(e)
|