Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -116,7 +116,7 @@ class WatermarkProcessor:
|
|
116 |
return image_path, f"Error in encoding: {str(e)}"
|
117 |
|
118 |
def decode(self, image_path):
|
119 |
-
"""Decode watermark with
|
120 |
try:
|
121 |
# Try PNG metadata method first
|
122 |
try:
|
@@ -139,8 +139,8 @@ class WatermarkProcessor:
|
|
139 |
for pixel in row:
|
140 |
for value in pixel:
|
141 |
binary_data += format(value, '08b')[-1]
|
142 |
-
|
143 |
-
# Convert to bytes
|
144 |
bytes_data = bytearray()
|
145 |
for i in range(0, len(binary_data), 8):
|
146 |
if i + 8 <= len(binary_data):
|
@@ -149,32 +149,23 @@ class WatermarkProcessor:
|
|
149 |
bytes_data.append(byte)
|
150 |
except ValueError:
|
151 |
continue
|
152 |
-
|
153 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
try:
|
155 |
-
#
|
156 |
-
decoded_data =
|
157 |
-
|
158 |
-
# Clean up decoded data
|
159 |
-
decoded_data = ''.join(char for char in decoded_data if ord(char) < 128 or ord(char) > 160)
|
160 |
|
161 |
-
# Find markers
|
162 |
-
end_marker = "====="
|
163 |
-
delimiter = "#####"
|
164 |
-
|
165 |
-
if end_marker in decoded_data:
|
166 |
-
parts = decoded_data.split(end_marker)
|
167 |
-
decoded_data = parts[0] # Take the part before =====
|
168 |
-
|
169 |
-
if delimiter in decoded_data:
|
170 |
-
parts = decoded_data.split(delimiter)
|
171 |
-
decoded_data = parts[0] # Take the part before #####
|
172 |
-
|
173 |
-
# Clean the decoded data
|
174 |
-
decoded_data = decoded_data.strip()
|
175 |
-
|
176 |
-
# Try to parse as JSON
|
177 |
try:
|
|
|
178 |
watermark_data = json.loads(decoded_data)
|
179 |
# Verify image hash
|
180 |
image_copy = image.copy() & 0xFE
|
@@ -183,35 +174,29 @@ class WatermarkProcessor:
|
|
183 |
if current_hash != watermark_data.get('image_hash'):
|
184 |
return "Warning: Image has been modified after watermarking"
|
185 |
|
|
|
186 |
return json.dumps(watermark_data, indent=2, ensure_ascii=False)
|
187 |
except json.JSONDecodeError:
|
188 |
-
# If not
|
189 |
-
return decoded_data
|
190 |
|
191 |
except UnicodeDecodeError:
|
192 |
-
#
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
for byte in bytes_data:
|
198 |
-
current_bytes.append(byte)
|
199 |
try:
|
200 |
-
char =
|
201 |
-
|
202 |
-
|
|
|
203 |
except UnicodeDecodeError:
|
204 |
-
if
|
205 |
-
|
206 |
continue
|
207 |
-
|
208 |
-
|
209 |
-
return fallback_decoded.strip()
|
210 |
-
else:
|
211 |
-
return "Error: Could not decode watermark data"
|
212 |
-
|
213 |
-
except Exception as e:
|
214 |
-
return f"Error in fallback decoding: {str(e)}"
|
215 |
|
216 |
except Exception as e:
|
217 |
return f"Error in decoding: {str(e)}"
|
|
|
116 |
return image_path, f"Error in encoding: {str(e)}"
|
117 |
|
118 |
def decode(self, image_path):
|
119 |
+
"""Decode watermark with simplified robust error handling"""
|
120 |
try:
|
121 |
# Try PNG metadata method first
|
122 |
try:
|
|
|
139 |
for pixel in row:
|
140 |
for value in pixel:
|
141 |
binary_data += format(value, '08b')[-1]
|
142 |
+
|
143 |
+
# Convert binary string to bytes
|
144 |
bytes_data = bytearray()
|
145 |
for i in range(0, len(binary_data), 8):
|
146 |
if i + 8 <= len(binary_data):
|
|
|
149 |
bytes_data.append(byte)
|
150 |
except ValueError:
|
151 |
continue
|
152 |
+
|
153 |
+
# Find end marker in raw bytes
|
154 |
+
end_sequence = b'====='
|
155 |
+
if end_sequence in bytes_data:
|
156 |
+
bytes_data = bytes_data[:bytes_data.index(end_sequence)]
|
157 |
+
|
158 |
+
# Find delimiter in raw bytes
|
159 |
+
delimiter = b'#####'
|
160 |
+
if delimiter in bytes_data:
|
161 |
+
bytes_data = bytes_data[:bytes_data.index(delimiter)]
|
162 |
+
|
163 |
try:
|
164 |
+
# Try direct UTF-8 decoding first
|
165 |
+
decoded_data = bytes_data.decode('utf-8')
|
|
|
|
|
|
|
166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
try:
|
168 |
+
# Try to parse as JSON
|
169 |
watermark_data = json.loads(decoded_data)
|
170 |
# Verify image hash
|
171 |
image_copy = image.copy() & 0xFE
|
|
|
174 |
if current_hash != watermark_data.get('image_hash'):
|
175 |
return "Warning: Image has been modified after watermarking"
|
176 |
|
177 |
+
# Return properly formatted JSON
|
178 |
return json.dumps(watermark_data, indent=2, ensure_ascii=False)
|
179 |
except json.JSONDecodeError:
|
180 |
+
# If not JSON, return decoded text
|
181 |
+
return decoded_data.strip()
|
182 |
|
183 |
except UnicodeDecodeError:
|
184 |
+
# If UTF-8 decoding fails, try character by character
|
185 |
+
result = []
|
186 |
+
i = 0
|
187 |
+
while i < len(bytes_data):
|
188 |
+
for j in range(min(4, len(bytes_data) - i), 0, -1):
|
|
|
|
|
189 |
try:
|
190 |
+
char = bytes_data[i:i+j].decode('utf-8')
|
191 |
+
result.append(char)
|
192 |
+
i += j
|
193 |
+
break
|
194 |
except UnicodeDecodeError:
|
195 |
+
if j == 1: # If even single byte fails
|
196 |
+
i += 1
|
197 |
continue
|
198 |
+
|
199 |
+
return ''.join(result).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
|
201 |
except Exception as e:
|
202 |
return f"Error in decoding: {str(e)}"
|