Spaces:
Running
Running
Update utils.py
Browse files
utils.py
CHANGED
@@ -21,7 +21,7 @@ class WatermarkProcessor:
|
|
21 |
def to_bin(self, data):
|
22 |
"""Convert data to binary format as string"""
|
23 |
if isinstance(data, str):
|
24 |
-
return ''.join(format(
|
25 |
elif isinstance(data, bytes):
|
26 |
return ''.join(format(x, '08b') for x in data)
|
27 |
elif isinstance(data, np.ndarray):
|
@@ -130,43 +130,33 @@ class WatermarkProcessor:
|
|
130 |
if image is None:
|
131 |
raise ValueError("Could not read image file")
|
132 |
|
133 |
-
# Extract LSB
|
134 |
-
|
135 |
-
total_bits = image.shape[0] * image.shape[1] * 3
|
136 |
-
|
137 |
for i in range(image.shape[0]):
|
138 |
for j in range(image.shape[1]):
|
139 |
for k in range(3):
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
except ValueError:
|
157 |
-
continue
|
158 |
-
|
159 |
-
try:
|
160 |
-
# Try UTF-8 decoding
|
161 |
-
decoded_text = bytes_data.decode('utf-8')
|
162 |
try:
|
163 |
-
# Try
|
164 |
-
|
165 |
-
return json.dumps(
|
166 |
except json.JSONDecodeError:
|
167 |
-
return
|
168 |
-
except UnicodeDecodeError:
|
169 |
-
return "Error: Invalid character encoding"
|
170 |
|
171 |
return "Error: No valid watermark found"
|
172 |
|
|
|
21 |
def to_bin(self, data):
|
22 |
"""Convert data to binary format as string"""
|
23 |
if isinstance(data, str):
|
24 |
+
return ''.join(format(ord(char), '08b') for char in data)
|
25 |
elif isinstance(data, bytes):
|
26 |
return ''.join(format(x, '08b') for x in data)
|
27 |
elif isinstance(data, np.ndarray):
|
|
|
130 |
if image is None:
|
131 |
raise ValueError("Could not read image file")
|
132 |
|
133 |
+
# Extract LSB data
|
134 |
+
binary_string = ''
|
|
|
|
|
135 |
for i in range(image.shape[0]):
|
136 |
for j in range(image.shape[1]):
|
137 |
for k in range(3):
|
138 |
+
binary_string += str(image[i, j, k] & 1)
|
139 |
+
|
140 |
+
# Convert binary to text
|
141 |
+
chars = []
|
142 |
+
for i in range(0, len(binary_string), 8):
|
143 |
+
byte = binary_string[i:i+8]
|
144 |
+
if len(byte) == 8:
|
145 |
+
chars.append(chr(int(byte, 2)))
|
146 |
+
|
147 |
+
# Join all characters
|
148 |
+
text = ''.join(chars)
|
149 |
+
|
150 |
+
# Find end marker
|
151 |
+
if "###END###" in text:
|
152 |
+
text = text.split("###END###")[0]
|
153 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
try:
|
155 |
+
# Try to parse as JSON
|
156 |
+
data = json.loads(text)
|
157 |
+
return json.dumps(data, ensure_ascii=False, indent=2)
|
158 |
except json.JSONDecodeError:
|
159 |
+
return text
|
|
|
|
|
160 |
|
161 |
return "Error: No valid watermark found"
|
162 |
|