fantos commited on
Commit
3409808
·
verified ·
1 Parent(s): e78a28c

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +41 -46
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 data
136
- binary_text = ''
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
- bit = image[i, j, k] & 1
142
- binary_text += str(bit)
143
-
144
- # Convert binary to text
145
- text = ''
146
- # Process 8 bits at a time
147
- for i in range(0, len(binary_text)-8, 8):
148
- byte = binary_text[i:i+8]
149
- text += chr(int(byte, 2))
150
- # Check for end marker
151
- if text[-7:] == "###END###":
152
- extracted_text = text[:-7]
153
- try:
154
- # Attempt to parse as JSON
155
- data = json.loads(extracted_text)
156
- return json.dumps(data, ensure_ascii=False, indent=2)
157
- except:
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
- data = json.loads(json_text)
176
- return json.dumps(data, ensure_ascii=False, indent=2)
177
- except:
178
- pass
179
-
180
- except:
181
- pass
182
-
183
- return text if text.strip() else "Error: No valid watermark found"
 
 
 
 
 
 
 
 
 
 
 
 
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)}"