Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -1,36 +1,49 @@
|
|
1 |
-
import cv2
|
2 |
import numpy as np
|
3 |
from PIL import Image, PngImagePlugin
|
|
|
|
|
4 |
|
5 |
-
def png_encode(im_name,extra):
|
6 |
-
|
7 |
-
|
8 |
im = Image.open(im_name)
|
9 |
-
|
10 |
info = PngImagePlugin.PngInfo()
|
11 |
info.add_text("TXT", extra)
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
|
18 |
def to_bin(data):
|
19 |
-
"""Convert `data` to binary format as string"""
|
20 |
if isinstance(data, str):
|
21 |
-
return ''.join([
|
22 |
elif isinstance(data, bytes):
|
23 |
-
return ''.join([
|
24 |
elif isinstance(data, np.ndarray):
|
25 |
-
return [
|
26 |
elif isinstance(data, int) or isinstance(data, np.uint8):
|
27 |
return format(data, "08b")
|
28 |
else:
|
29 |
raise TypeError("Type not supported.")
|
30 |
-
|
|
|
|
|
31 |
try:
|
32 |
BGRimage = cv2.imread(f"{image_name}")
|
33 |
-
image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)
|
34 |
binary_data = ""
|
35 |
for row in image:
|
36 |
for pixel in row:
|
@@ -38,34 +51,34 @@ def decode(image_name,txt=None):
|
|
38 |
binary_data += r[-1]
|
39 |
binary_data += g[-1]
|
40 |
binary_data += b[-1]
|
41 |
-
all_bytes = [
|
42 |
decoded_data = ""
|
43 |
for byte in all_bytes:
|
44 |
decoded_data += chr(int(byte, 2))
|
45 |
if decoded_data[-5:] == "=====":
|
46 |
break
|
47 |
-
this = decoded_data[:-5].split("#####",1)[0]
|
48 |
-
|
49 |
except Exception as e:
|
50 |
-
|
51 |
-
|
52 |
|
53 |
-
def encode(image_name, secret_data,txt=None):
|
54 |
-
msg=""
|
55 |
BGRimage = cv2.imread(f"{image_name}")
|
56 |
-
image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)
|
57 |
n_bytes = image.shape[0] * image.shape[1] * 3 // 8
|
58 |
print("[*] Maximum bytes to encode:", n_bytes)
|
59 |
-
secret_data1=secret_data
|
60 |
while True:
|
61 |
-
if len(secret_data1)+5 < (n_bytes):
|
62 |
secret_data1 = f'{secret_data1}#####'
|
63 |
-
elif len(secret_data1)+5 >= (n_bytes):
|
64 |
-
break
|
65 |
-
secret_data = secret_data1
|
66 |
if len(secret_data) > n_bytes:
|
67 |
-
msg ="Watermark is too large for Image Size"
|
68 |
-
return image_name,msg
|
69 |
secret_data += "====="
|
70 |
data_index = 0
|
71 |
binary_secret_data = to_bin(secret_data)
|
@@ -84,4 +97,4 @@ def encode(image_name, secret_data,txt=None):
|
|
84 |
data_index += 1
|
85 |
if data_index >= data_len:
|
86 |
break
|
87 |
-
return image,msg
|
|
|
1 |
+
import cv2
|
2 |
import numpy as np
|
3 |
from PIL import Image, PngImagePlugin
|
4 |
+
import os
|
5 |
+
import uuid
|
6 |
|
7 |
+
def png_encode(im_name, extra):
|
8 |
+
try:
|
|
|
9 |
im = Image.open(im_name)
|
|
|
10 |
info = PngImagePlugin.PngInfo()
|
11 |
info.add_text("TXT", extra)
|
12 |
+
|
13 |
+
unique_id = str(uuid.uuid4())[:8]
|
14 |
+
filename, ext = os.path.splitext(im_name)
|
15 |
+
new_filename = f"{filename}_{unique_id}{ext}"
|
16 |
+
im.save(new_filename, pnginfo=info)
|
17 |
+
|
18 |
+
width, height = im.size
|
19 |
+
rect_width, rect_height = 200, 50
|
20 |
+
x = width - rect_width - 10
|
21 |
+
y = height - rect_height - 10
|
22 |
+
png_encode_coords = (x, y, rect_width, rect_height)
|
23 |
+
|
24 |
+
return new_filename, "", png_encode_coords
|
25 |
+
|
26 |
+
except Exception as e:
|
27 |
+
return None, str(e), None
|
28 |
|
29 |
|
30 |
def to_bin(data):
|
|
|
31 |
if isinstance(data, str):
|
32 |
+
return ''.join([format(ord(i), "08b") for i in data])
|
33 |
elif isinstance(data, bytes):
|
34 |
+
return ''.join([format(i, "08b") for i in data])
|
35 |
elif isinstance(data, np.ndarray):
|
36 |
+
return [format(i, "08b") for i in data]
|
37 |
elif isinstance(data, int) or isinstance(data, np.uint8):
|
38 |
return format(data, "08b")
|
39 |
else:
|
40 |
raise TypeError("Type not supported.")
|
41 |
+
|
42 |
+
|
43 |
+
def decode(image_name, txt=None):
|
44 |
try:
|
45 |
BGRimage = cv2.imread(f"{image_name}")
|
46 |
+
image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)
|
47 |
binary_data = ""
|
48 |
for row in image:
|
49 |
for pixel in row:
|
|
|
51 |
binary_data += r[-1]
|
52 |
binary_data += g[-1]
|
53 |
binary_data += b[-1]
|
54 |
+
all_bytes = [binary_data[i: i + 8] for i in range(0, len(binary_data), 8)]
|
55 |
decoded_data = ""
|
56 |
for byte in all_bytes:
|
57 |
decoded_data += chr(int(byte, 2))
|
58 |
if decoded_data[-5:] == "=====":
|
59 |
break
|
60 |
+
this = decoded_data[:-5].split("#####", 1)[0]
|
61 |
+
return this
|
62 |
except Exception as e:
|
63 |
+
return str(e)
|
64 |
+
|
65 |
|
66 |
+
def encode(image_name, secret_data, txt=None):
|
67 |
+
msg = ""
|
68 |
BGRimage = cv2.imread(f"{image_name}")
|
69 |
+
image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)
|
70 |
n_bytes = image.shape[0] * image.shape[1] * 3 // 8
|
71 |
print("[*] Maximum bytes to encode:", n_bytes)
|
72 |
+
secret_data1 = secret_data
|
73 |
while True:
|
74 |
+
if len(secret_data1) + 5 < (n_bytes):
|
75 |
secret_data1 = f'{secret_data1}#####'
|
76 |
+
elif len(secret_data1) + 5 >= (n_bytes):
|
77 |
+
break
|
78 |
+
secret_data = secret_data1
|
79 |
if len(secret_data) > n_bytes:
|
80 |
+
msg = "Watermark is too large for Image Size"
|
81 |
+
return image_name, msg
|
82 |
secret_data += "====="
|
83 |
data_index = 0
|
84 |
binary_secret_data = to_bin(secret_data)
|
|
|
97 |
data_index += 1
|
98 |
if data_index >= data_len:
|
99 |
break
|
100 |
+
return image, msg
|