Spaces:
Running
Running
Update utils.py
Browse files
utils.py
CHANGED
@@ -132,55 +132,50 @@ class WatermarkProcessor:
|
|
132 |
if image is None:
|
133 |
raise ValueError("Could not read image file")
|
134 |
|
135 |
-
# Extract LSB
|
136 |
-
|
137 |
-
# Get LSB from each byte of each pixel
|
138 |
for i in range(image.shape[0]):
|
139 |
for j in range(image.shape[1]):
|
140 |
-
for k in range(3):
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
# If not valid JSON, return as plain text
|
159 |
-
return extracted_text.strip()
|
160 |
-
|
161 |
-
# If we got here, try to find any valid JSON in the text
|
162 |
-
try:
|
163 |
-
# Convert remaining bits
|
164 |
-
for i in range(0, len(binary_text), 8):
|
165 |
-
if i + 8 <= len(binary_text):
|
166 |
-
byte = binary_text[i:i+8]
|
167 |
-
text += chr(int(byte, 2))
|
168 |
-
|
169 |
-
# Look for JSON markers
|
170 |
-
start = text.find('{')
|
171 |
-
end = text.rfind('}')
|
172 |
-
if start != -1 and end != -1:
|
173 |
-
json_text = text[start:end+1]
|
174 |
try:
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
|
185 |
except Exception as e:
|
186 |
return f"Error in decoding: {str(e)}"
|
|
|
132 |
if image is None:
|
133 |
raise ValueError("Could not read image file")
|
134 |
|
135 |
+
# Extract LSB from image
|
136 |
+
binary_data = []
|
|
|
137 |
for i in range(image.shape[0]):
|
138 |
for j in range(image.shape[1]):
|
139 |
+
for k in range(3): # RGB channels
|
140 |
+
binary_data.append(str(image[i, j, k] & 1))
|
141 |
+
|
142 |
+
# Convert bits to bytes
|
143 |
+
bytes_data = bytearray()
|
144 |
+
for i in range(0, len(binary_data), 8):
|
145 |
+
if i + 8 <= len(binary_data):
|
146 |
+
byte = ''.join(binary_data[i:i+8])
|
147 |
+
bytes_data.append(int(byte, 2))
|
148 |
+
|
149 |
+
# Find end marker in raw bytes
|
150 |
+
end_marker = b'###END###'
|
151 |
+
if end_marker in bytes_data:
|
152 |
+
# Extract data before end marker
|
153 |
+
data = bytes_data[:bytes_data.index(end_marker)]
|
154 |
+
try:
|
155 |
+
# Try to decode as UTF-8
|
156 |
+
decoded_text = data.decode('utf-8', errors='ignore')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
try:
|
158 |
+
# Try to parse as JSON
|
159 |
+
json_data = json.loads(decoded_text)
|
160 |
+
return json.dumps(json_data, ensure_ascii=False, indent=2)
|
161 |
+
except json.JSONDecodeError:
|
162 |
+
return decoded_text
|
163 |
+
except UnicodeDecodeError:
|
164 |
+
# If UTF-8 decoding fails, try decoding parts
|
165 |
+
result = ''
|
166 |
+
buffer = bytearray()
|
167 |
+
for byte in data:
|
168 |
+
buffer.append(byte)
|
169 |
+
try:
|
170 |
+
result += buffer.decode('utf-8')
|
171 |
+
buffer.clear()
|
172 |
+
except UnicodeDecodeError:
|
173 |
+
if len(buffer) >= 4: # Max UTF-8 character length
|
174 |
+
buffer.clear()
|
175 |
+
continue
|
176 |
+
return result if result else "Error: Invalid character encoding"
|
177 |
+
|
178 |
+
return "Error: No valid watermark found"
|
179 |
|
180 |
except Exception as e:
|
181 |
return f"Error in decoding: {str(e)}"
|