fantos commited on
Commit
50e2309
·
verified ·
1 Parent(s): 6397ae5

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +23 -33
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(x, '08b') for x in data.encode('utf-8'))
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 and build binary string
134
- binary_data = ""
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
- if len(binary_data) >= total_bits:
141
- break
142
- binary_data += str(image[i, j, k] & 1)
143
-
144
- # Convert binary to bytes
145
- bytes_data = bytearray()
146
- for i in range(0, len(binary_data), 8):
147
- if i + 8 <= len(binary_data):
148
- byte = binary_data[i:i+8]
149
- bytes_data.append(int(byte, 2))
150
-
151
- # Early stopping if we find the end marker
152
- try:
153
- if b'###END###' in bytes_data:
154
- bytes_data = bytes_data[:bytes_data.index(b'###END###')]
155
- break
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 parsing as JSON
164
- json_data = json.loads(decoded_text)
165
- return json.dumps(json_data, ensure_ascii=False, indent=2)
166
  except json.JSONDecodeError:
167
- return decoded_text
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