Omnibus commited on
Commit
bc1c656
·
1 Parent(s): 9d256bd

Upload 6 files

Browse files
Files changed (6) hide show
  1. overlay.py +104 -0
  2. private_key.png +0 -0
  3. public_key.png +0 -0
  4. qr.py +83 -0
  5. stegan.py +108 -0
  6. stegan2.py +106 -0
overlay.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ from PIL import ImageFont, ImageDraw, Image
3
+
4
+ def process(img,font_text,font_fac,font_x,font_y,font_col,font_op):
5
+ img.save('tmp.png')
6
+
7
+ img = Image.open('tmp.png').convert("RGBA")
8
+ im=img
9
+ #im=Image.open(img)
10
+ txt = Image.new('RGBA', im.size, (255,255,255,0))
11
+
12
+ w, h = im.size
13
+ print (f'FONT COLOR: {font_col}')
14
+ #t_fill = ("#"+"E6"+f"{font_col.strip('#')}")
15
+
16
+ #t_fill = (font_col)
17
+ h1 = font_col.strip("#")
18
+ rgb_tup = tuple(int(h1[i:i+2], 16) for i in (0, 2, 4))
19
+ print (rgb_tup)
20
+ a,b,c = rgb_tup
21
+ t_fill = (a,b,c,font_op)
22
+
23
+ print (f'FONT COLOR: {t_fill}')
24
+
25
+ #x = int(w-font_x)
26
+ #y = int(h-font_y)
27
+ x = int(font_x)
28
+ y = int(font_y)
29
+ draw = ImageDraw.Draw(txt)
30
+ text = f'{font_text}'
31
+ font_size=font_fac
32
+ font = ImageFont.truetype("./fonts/SansitaOne.ttf", int(font_size))
33
+ size = font.getsize(text)
34
+ draw.text((x-size[0]/2, y),text, font = font, fill=t_fill)
35
+
36
+ #txt.putalpha(128)
37
+
38
+ combined = Image.alpha_composite(im, txt)
39
+ return combined
40
+
41
+ def textover(im,txt1="",txt2=""):
42
+ #im.save('tmp.png')
43
+
44
+ im = Image.open(im)
45
+ inp=1
46
+ hh=0
47
+ hhh=25
48
+ #cnt = len(inp)
49
+ cnt = inp
50
+ font_a = 30
51
+ font_b = 10
52
+ if cnt >0:
53
+ font_a = font_a + (cnt * 2)
54
+ font_b = font_b + (cnt * 2)
55
+ #hh = hh-int(cnt/2)
56
+ hhh = hhh+int(cnt/2)
57
+ w,h = im.size
58
+ print (w)
59
+ print (h)
60
+ font_x = (w/2)
61
+ font_y = h-hhh
62
+ out = process(im,txt1,font_fac=font_a,font_x=font_x,font_y=hh,font_col="#000000",font_op=255)
63
+ out = process(out,txt2,font_fac=font_b,font_x=font_x,font_y=font_y,font_col="#000000",font_op=255)
64
+ #out.save("out.png")
65
+
66
+ return out
67
+
68
+ def background_image():
69
+ img_b = np.zeros([512,512,3],dtype=np.uint8)
70
+ img_b.fill(255)
71
+ img_b.save("background.png")
72
+
73
+ def custom_overlay(txt=None):
74
+ background_image()
75
+ background = Image.open("background.png")
76
+ x,y = background.size
77
+ if x > y:
78
+ aa=y
79
+ if y > x:
80
+ aa=x
81
+ if x==y:
82
+ aa=x
83
+ out = f'{txt}'
84
+ print (f'txt {out}')
85
+ qrm = qr.QRCode(box_size=10,error_correction=qr.constants.ERROR_CORRECT_H)
86
+ qrm.add_data(out)
87
+ qrm.make(fit=True)
88
+ img1 = qrm.make_image(fill_color=fill, back_color="white")
89
+ img1 = img1.resize((aa,aa))
90
+ img1.save("im2.png")
91
+
92
+ image_bgr = cv2.imread('im2.png')
93
+
94
+ h, w, c = image_bgr.shape
95
+ image_bgra = np.concatenate([image_bgr, np.full((h, w, 1), 255, dtype=np.uint8)], axis=-1)
96
+ white = np.all(image_bgr == [255, 255, 255], axis=-1)
97
+ image_bgra[white, -1] = 0
98
+ cv2.imwrite('result.png', image_bgra)
99
+
100
+
101
+ over_im = Image.open("result.png")
102
+ background.paste(over_im, (0, 0), over_im)
103
+ background.save("out_im.png")
104
+ return "out_im.png"
private_key.png ADDED
public_key.png ADDED
qr.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import qrcode as qr
2
+ import base64
3
+ import cv2
4
+ import os
5
+ from PIL import Image
6
+
7
+ def make_qr(txt=None,data=None,im_size=None,color_f=None,color_b=None):
8
+
9
+ qrm = qr.QRCode(box_size=10,error_correction=qr.constants.ERROR_CORRECT_H)
10
+ if color_f == None:
11
+ color_f = "#000"
12
+ if color_b == None:
13
+ color_b = "#fff"
14
+ if txt != None and txt != "" and data != None:
15
+ f = Image.open(f'{data}')
16
+ f.thumbnail((im_size,im_size))
17
+ f.save("tmp.jpg")
18
+ imr = open(f'tmp.jpg','rb')
19
+ out = f'{txt}+++{base64.b64encode(imr.read())}'
20
+ print (f'txt+data {out}')
21
+ qrm.add_data(out)
22
+ qrm.make(fit=True)
23
+ img1 = qrm.make_image(fill_color=color_f, back_color=color_b)
24
+ img1.save("im.png")
25
+ return "im.png"
26
+ if txt == None or txt == "" and data != None:
27
+ f = Image.open(f'{data}')
28
+ f.thumbnail((im_size,im_size))
29
+ f.save("tmp1.jpg")
30
+ imr = open(f'tmp1.jpg','rb')
31
+ out = f'+++{base64.b64encode(imr.read())}'
32
+ print (f'data {out}')
33
+ qrm.add_data(out)
34
+ qrm.make(fit=True)
35
+ img1 = qrm.make_image(fill_color=color_f, back_color=color_b)
36
+ img1.save("im1.png")
37
+ return "im1.png"
38
+
39
+ if txt != None and txt != "" and data == None:
40
+ out = f'{txt}'
41
+ print (f'txt {out}')
42
+ qrm.add_data(out)
43
+ qrm.make(fit=True)
44
+ img1 = qrm.make_image(fill_color=color_f, back_color=color_b)
45
+ img1.save("im2.png")
46
+ return "im2.png"
47
+
48
+ def cnt_im_bytes(im,txt_cnt,im_size):
49
+ f = Image.open(f'{im}')
50
+ f.thumbnail((im_size,im_size))
51
+ f.save("tmp11.jpg")
52
+ im_cnt=os.stat('tmp11.jpg').st_size
53
+ print(im_cnt)
54
+ tot_cnt=im_cnt+int(txt_cnt)
55
+ return im_cnt,tot_cnt
56
+
57
+ def cnt_bytes(txt,im_cnt):
58
+ txt_cnt = (len(txt.encode('utf-8')))
59
+ tot_cnt = txt_cnt + int(im_cnt)
60
+ return txt_cnt, tot_cnt
61
+
62
+
63
+ def decode(im):
64
+
65
+ image = cv2.imread(f'{im}')
66
+ qrCodeDetector = cv2.QRCodeDetector()
67
+ decodedText, points, _ = qrCodeDetector.detectAndDecode(image)
68
+ if points is not None:
69
+ text = decodedText
70
+ else:
71
+ text = "No QR Code Found"
72
+ return text
73
+ def make_im(tx_str):
74
+ out = tx_str.split("+++b",1)[1]
75
+ out.replace("'","")
76
+ print(out)
77
+ decoded_data=base64.b64decode((out))
78
+
79
+ #write the decoded data back to original format in file
80
+ img_file = open('image.jpeg', 'wb')
81
+ img_file.write(decoded_data)
82
+ img_file.close()
83
+ return ('image.jpeg')
stegan.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import cv2
3
+ import numpy as np
4
+ #import pandas as pd
5
+ from PIL import Image
6
+ import os
7
+ import uuid as uniq
8
+ from qr import make_qr
9
+ import math
10
+
11
+
12
+ def to_bin(data):
13
+ """Convert `data` to binary format as string"""
14
+ if isinstance(data, str):
15
+ return ''.join([ format(ord(i), "08b") for i in data ])
16
+ elif isinstance(data, bytes):
17
+ return ''.join([ format(i, "08b") for i in data ])
18
+ elif isinstance(data, np.ndarray):
19
+ return [ format(i, "08b") for i in data ]
20
+ elif isinstance(data, int) or isinstance(data, np.uint8):
21
+ return format(data, "08b")
22
+ else:
23
+ raise TypeError("Type not supported.")
24
+ def decode(image_name,txt=None):
25
+ BGRimage = cv2.imread(image_name)
26
+ image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)
27
+ binary_data = ""
28
+ for row in image:
29
+ for pixel in row:
30
+ r, g, b = to_bin(pixel)
31
+ binary_data += r[-1]
32
+ binary_data += g[-1]
33
+ binary_data += b[-1]
34
+ all_bytes = [ binary_data[i: i+8] for i in range(0, len(binary_data), 8) ]
35
+ decoded_data = ""
36
+ for byte in all_bytes:
37
+ decoded_data += chr(int(byte, 2))
38
+ if decoded_data[-5:] == "=====":
39
+ break
40
+ this = decoded_data[:-5].split("#####",1)[0]
41
+ this = eval(this)
42
+ enc_in=this
43
+ return this
44
+
45
+ def encode(image_name, secret_data,txt=None):
46
+ BGRimage = cv2.imread(image_name)
47
+ image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)
48
+ n_bytes = image.shape[0] * image.shape[1] * 3 // 8
49
+ print("[*] Maximum bytes to encode:", n_bytes)
50
+ secret_data1=secret_data
51
+ #secret_data1=f'{secret_data}#{resultp}'
52
+
53
+ while True:
54
+ if len(secret_data1)+5 < (n_bytes):
55
+ secret_data1 = f'{secret_data1}#####'
56
+ elif len(secret_data1)+5 >= (n_bytes):
57
+ break
58
+ secret_data = secret_data1
59
+ if len(secret_data) > n_bytes:
60
+ return image_name, gr.Markdown.update("""<center><h3>Input image is too large""")
61
+ secret_data += "====="
62
+ data_index = 0
63
+ binary_secret_data = to_bin(secret_data)
64
+ data_len = len(binary_secret_data)
65
+ for row in image:
66
+ for pixel in row:
67
+ r, g, b = to_bin(pixel)
68
+ if data_index < data_len:
69
+ pixel[0] = int(r[:-1] + binary_secret_data[data_index], 2)
70
+ data_index += 1
71
+ if data_index < data_len:
72
+ pixel[1] = int(g[:-1] + binary_secret_data[data_index], 2)
73
+ data_index += 1
74
+ if data_index < data_len:
75
+ pixel[2] = int(b[:-1] + binary_secret_data[data_index], 2)
76
+ data_index += 1
77
+ if data_index >= data_len:
78
+ break
79
+ return image
80
+ def conv_im(im,data):
81
+ uniqnum = uniq.uuid4()
82
+
83
+ byte_size = len(data)
84
+ print (f'bytes:{byte_size}')
85
+ data_pixels = byte_size*4
86
+ print (f'pixels:{data_pixels}')
87
+ #data_sq=data_pixels/2
88
+ data_sq = int(math.sqrt(data_pixels))
89
+ data_pad = data_sq+100
90
+ print (f'square image:{data_pad}x{data_pad}')
91
+
92
+ qr_im = im
93
+ img1 = Image.open(qr_im)
94
+ imgw = img1.size[0]
95
+ imgh = img1.size[1]
96
+ print (f'qr Size:{img1.size}')
97
+ #img1.thumbnail((imgw*4,imgh*4), Image.Resampling.LANCZOS)
98
+ if data_pad > imgw or data_pad > imgh :
99
+
100
+ img1 = img1.resize((int(data_pad),int(data_pad)), Image.Resampling.LANCZOS)
101
+ print (img1.size)
102
+ img1.save(f'tmpim{uniqnum}.png')
103
+
104
+ with open(f'tmpim{uniqnum}.png', "rb") as image_file:
105
+ encoded_string = base64.b64encode(image_file.read())
106
+ image_file.close()
107
+ im_out = encode(f'tmpim{uniqnum}.png',data)
108
+ return im_out
stegan2.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import cv2
3
+ import numpy as np
4
+ #import pandas as pd
5
+ from PIL import Image
6
+ import os
7
+ import uuid as uniq
8
+ from qr import make_qr
9
+ import math
10
+
11
+
12
+ def to_bin(data):
13
+ """Convert `data` to binary format as string"""
14
+ if isinstance(data, str):
15
+ return ''.join([ format(ord(i), "08b") for i in data ])
16
+ elif isinstance(data, bytes):
17
+ return ''.join([ format(i, "08b") for i in data ])
18
+ elif isinstance(data, np.ndarray):
19
+ return [ format(i, "08b") for i in data ]
20
+ elif isinstance(data, int) or isinstance(data, np.uint8):
21
+ return format(data, "08b")
22
+ else:
23
+ raise TypeError("Type not supported.")
24
+ def decode(image_name,txt=None):
25
+ BGRimage = cv2.imread(image_name)
26
+ image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)
27
+ binary_data = ""
28
+ for row in image:
29
+ for pixel in row:
30
+ r, g, b = to_bin(pixel)
31
+ binary_data += r[-1]
32
+ binary_data += g[-1]
33
+ binary_data += b[-1]
34
+ all_bytes = [ binary_data[i: i+8] for i in range(0, len(binary_data), 8) ]
35
+ decoded_data = ""
36
+ for byte in all_bytes:
37
+ decoded_data += chr(int(byte, 2))
38
+ if decoded_data[-5:] == "=====":
39
+ break
40
+ this = decoded_data[:-5].split("#####",1)[0]
41
+ this = eval(this)
42
+ enc_in=this
43
+ return this
44
+
45
+ def encode(image_name, secret_data,txt=None):
46
+ BGRimage = cv2.imread(image_name)
47
+ image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)
48
+ n_bytes = image.shape[0] * image.shape[1] * 3 // 8
49
+ print("[*] Maximum bytes to encode:", n_bytes)
50
+ secret_data1=secret_data
51
+ #secret_data1=f'{secret_data}#{resultp}'
52
+
53
+ while True:
54
+ if len(secret_data1)+5 < (n_bytes):
55
+ secret_data1 = f'{secret_data1}#####'
56
+ elif len(secret_data1)+5 >= (n_bytes):
57
+ break
58
+ secret_data = secret_data1
59
+ if len(secret_data) > n_bytes:
60
+ return image_name, gr.Markdown.update("""<center><h3>Input image is too large""")
61
+ secret_data += "====="
62
+ data_index = 0
63
+ binary_secret_data = to_bin(secret_data)
64
+ data_len = len(binary_secret_data)
65
+ for row in image:
66
+ for pixel in row:
67
+ r, g, b = to_bin(pixel)
68
+ if data_index < data_len:
69
+ pixel[0] = int(r[:-1] + binary_secret_data[data_index], 2)
70
+ data_index += 1
71
+ if data_index < data_len:
72
+ pixel[1] = int(g[:-1] + binary_secret_data[data_index], 2)
73
+ data_index += 1
74
+ if data_index < data_len:
75
+ pixel[2] = int(b[:-1] + binary_secret_data[data_index], 2)
76
+ data_index += 1
77
+ if data_index >= data_len:
78
+ break
79
+ return image
80
+ def conv_im(qr_link,data):
81
+ uniqnum = uniq.uuid4()
82
+
83
+ byte_size = len(data)
84
+ print (f'bytes:{byte_size}')
85
+ data_pixels = byte_size*4
86
+ print (f'pixels:{data_pixels}')
87
+ #data_sq=data_pixels/2
88
+ data_sq = int(math.sqrt(data_pixels))
89
+ data_pad = data_sq+100
90
+ print (f'square image:{data_pad}x{data_pad}')
91
+
92
+ qr_im = make_qr(txt=qr_link)
93
+ img1 = Image.open(qr_im)
94
+ imgw = img1.size[0]
95
+ imgh = img1.size[1]
96
+ print (f'qr Size:{img1.size}')
97
+ #img1.thumbnail((imgw*4,imgh*4), Image.Resampling.LANCZOS)
98
+ img1 = img1.resize((int(data_pad),int(data_pad)), Image.Resampling.LANCZOS)
99
+ print (img1.size)
100
+ img1.save(f'tmpim{uniqnum}.png')
101
+
102
+ with open(f'tmpim{uniqnum}.png', "rb") as image_file:
103
+ encoded_string = base64.b64encode(image_file.read())
104
+ image_file.close()
105
+ im_out = encode(f'tmpim{uniqnum}.png',data)
106
+ return im_out