noumanjavaid commited on
Commit
db1515c
·
verified ·
1 Parent(s): e7ccac1

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +34 -42
utils.py CHANGED
@@ -22,44 +22,43 @@ def encode(image_path, secret_data):
22
  image = cv2.imread(image_path)
23
  if image is None:
24
  return None, "Failed to read image"
25
-
26
  # Calculate maximum bytes for encoding
27
- n_bytes = (image.shape[0] * image.shape[1] * 3) // 8
28
 
29
- # Prepare secret data
30
  secret_data_with_delimiter = f'{secret_data}#####'
31
  if len(secret_data_with_delimiter) + 5 >= n_bytes:
32
  return None, "Watermark is too large for Image Size"
33
-
 
34
  secret_data_with_delimiter += "====="
 
 
35
  binary_secret_data = to_bin(secret_data_with_delimiter)
36
  data_len = len(binary_secret_data)
37
-
38
- # Create a copy of the image
39
- watermarked_image = image.copy()
40
  data_index = 0
41
 
42
- # Encode the data
43
- for i in range(watermarked_image.shape[0]):
44
- for j in range(watermarked_image.shape[1]):
45
- for k in range(3): # RGB channels
 
46
  if data_index < data_len:
47
- # Get the current pixel value
48
- pixel = watermarked_image[i, j, k]
49
- # Convert to binary and modify the least significant bit
50
- binary_pixel = format(pixel, '08b')
51
- new_pixel = binary_pixel[:-1] + binary_secret_data[data_index]
52
- # Update the pixel value
53
- watermarked_image[i, j, k] = int(new_pixel, 2)
54
  data_index += 1
55
- else:
56
- break
57
  if data_index >= data_len:
58
  break
59
  if data_index >= data_len:
60
  break
61
-
62
- return watermarked_image, None
63
  except Exception as e:
64
  return None, f"Error during encoding: {str(e)}"
65
 
@@ -69,30 +68,23 @@ def decode(image_path):
69
  image = cv2.imread(image_path)
70
  if image is None:
71
  return "Failed to read image"
72
-
73
  binary_data = ""
74
- # Extract the least significant bits
75
- for i in range(image.shape[0]):
76
- for j in range(image.shape[1]):
77
- for k in range(3):
78
- pixel = format(image[i, j, k], '08b')
79
- binary_data += pixel[-1]
80
 
81
  # Convert binary to text
 
82
  decoded_data = ""
83
- for i in range(0, len(binary_data), 8):
84
- byte = binary_data[i:i+8]
85
- if len(byte) == 8:
86
- decoded_data += chr(int(byte, 2))
87
- # Check for ending delimiter
88
- if decoded_data[-5:] == "=====":
89
- break
90
-
91
- # Remove delimiters and return the message
92
- if "#####" in decoded_data:
93
- return decoded_data.split("#####")[0]
94
- return "No watermark found"
95
 
 
 
96
  except Exception as e:
97
  return f"Error during decoding: {str(e)}"
98
 
@@ -117,4 +109,4 @@ def png_encode(im_name, extra):
117
  return new_filename, None
118
 
119
  except Exception as e:
120
- return None, str(e)
 
22
  image = cv2.imread(image_path)
23
  if image is None:
24
  return None, "Failed to read image"
25
+
26
  # Calculate maximum bytes for encoding
27
+ n_bytes = image.shape[0] * image.shape[1] * 3 // 8
28
 
29
+ # Check if the secret data is too large
30
  secret_data_with_delimiter = f'{secret_data}#####'
31
  if len(secret_data_with_delimiter) + 5 >= n_bytes:
32
  return None, "Watermark is too large for Image Size"
33
+
34
+ # Add ending delimiter
35
  secret_data_with_delimiter += "====="
36
+
37
+ # Convert data to binary
38
  binary_secret_data = to_bin(secret_data_with_delimiter)
39
  data_len = len(binary_secret_data)
 
 
 
40
  data_index = 0
41
 
42
+ # Encode the data into the image
43
+ for i in range(image.shape[0]):
44
+ for j in range(image.shape[1]):
45
+ pixel = image[i, j]
46
+ for color_channel in range(3):
47
  if data_index < data_len:
48
+ # Get the binary value of the pixel
49
+ binary_value = to_bin(pixel[color_channel])
50
+ # Replace the least significant bit
51
+ new_binary = binary_value[:-1] + binary_secret_data[data_index]
52
+ # Update the pixel with the new value
53
+ pixel[color_channel] = int(new_binary, 2)
 
54
  data_index += 1
55
+ image[i, j] = pixel
 
56
  if data_index >= data_len:
57
  break
58
  if data_index >= data_len:
59
  break
60
+
61
+ return image, None
62
  except Exception as e:
63
  return None, f"Error during encoding: {str(e)}"
64
 
 
68
  image = cv2.imread(image_path)
69
  if image is None:
70
  return "Failed to read image"
71
+
72
  binary_data = ""
73
+ for row in image:
74
+ for pixel in row:
75
+ for color_channel in pixel:
76
+ binary_data += to_bin(color_channel)[-1]
 
 
77
 
78
  # Convert binary to text
79
+ all_bytes = [binary_data[i: i + 8] for i in range(0, len(binary_data), 8)]
80
  decoded_data = ""
81
+ for byte in all_bytes:
82
+ decoded_data += chr(int(byte, 2))
83
+ if decoded_data[-5:] == "=====":
84
+ break
 
 
 
 
 
 
 
 
85
 
86
+ # Remove delimiters
87
+ return decoded_data[:-5].split("#####")[0]
88
  except Exception as e:
89
  return f"Error during decoding: {str(e)}"
90
 
 
109
  return new_filename, None
110
 
111
  except Exception as e:
112
+ return None, str(e)