Omnibus commited on
Commit
71c2a52
1 Parent(s): 7e5487d

Create stegan.py

Browse files
Files changed (1) hide show
  1. stegan.py +115 -0
stegan.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import gradio as gr
3
+ import cv2
4
+ import numpy as np
5
+ import pandas as pd
6
+ from PIL import Image
7
+ import hashlib
8
+ import os
9
+ import uuid as uniq
10
+ from qr import make_qr
11
+ import math
12
+
13
+
14
+ def to_bin(data):
15
+ """Convert `data` to binary format as string"""
16
+ if isinstance(data, str):
17
+ return ''.join([ format(ord(i), "08b") for i in data ])
18
+ elif isinstance(data, bytes):
19
+ return ''.join([ format(i, "08b") for i in data ])
20
+ elif isinstance(data, np.ndarray):
21
+ return [ format(i, "08b") for i in data ]
22
+ elif isinstance(data, int) or isinstance(data, np.uint8):
23
+ return format(data, "08b")
24
+ else:
25
+ raise TypeError("Type not supported.")
26
+ def decode(image_name,txt=None):
27
+ BGRimage = cv2.imread(image_name)
28
+ image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)
29
+ #resultd = hashlib.sha256(txt.encode('utf-8')).hexdigest()
30
+ binary_data = ""
31
+ for row in image:
32
+ for pixel in row:
33
+ r, g, b = to_bin(pixel)
34
+ binary_data += r[-1]
35
+ binary_data += g[-1]
36
+ binary_data += b[-1]
37
+ all_bytes = [ binary_data[i: i+8] for i in range(0, len(binary_data), 8) ]
38
+ decoded_data = ""
39
+ for byte in all_bytes:
40
+ decoded_data += chr(int(byte, 2))
41
+ if decoded_data[-5:] == "=====":
42
+ break
43
+ p = decoded_data[:-5].split("#",1)[1].split("#",1)[0]
44
+ #if p == resultd:
45
+ this = decoded_data[:-5].split("#",1)[0].split("'",1)[1]
46
+ #base = bytes(this, 'utf-8')
47
+ #with open(f"finished_im{uniqnum}.png", "wb") as fh:
48
+ # fh.write(base64.decodebytes(bytes(this, 'utf-8')))
49
+ #fh.close
50
+ #return f"finished_im{uniqnum}.png"
51
+ return this
52
+
53
+ def encode(image_name, secret_data,txt=None):
54
+ BGRimage = cv2.imread(image_name)
55
+ image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)
56
+ n_bytes = image.shape[0] * image.shape[1] * 3 // 8
57
+ print("[*] Maximum bytes to encode:", n_bytes)
58
+ #resultp = hashlib.sha256(txt.encode('utf-8')).hexdigest()
59
+ secret_data1=secret_data
60
+ #secret_data1=f'{secret_data}#{resultp}'
61
+
62
+ while True:
63
+ if len(secret_data1) < (n_bytes):
64
+ secret_data1 = f'{secret_data1}#'
65
+ elif len(secret_data1) >= (n_bytes):
66
+ break
67
+ secret_data = secret_data1
68
+ if len(secret_data) > n_bytes:
69
+ return image_name, gr.Markdown.update("""<center><h3>Input image is too large""")
70
+ secret_data += "====="
71
+ data_index = 0
72
+ binary_secret_data = to_bin(secret_data)
73
+ data_len = len(binary_secret_data)
74
+ for row in image:
75
+ for pixel in row:
76
+ r, g, b = to_bin(pixel)
77
+ if data_index < data_len:
78
+ pixel[0] = int(r[:-1] + binary_secret_data[data_index], 2)
79
+ data_index += 1
80
+ if data_index < data_len:
81
+ pixel[1] = int(g[:-1] + binary_secret_data[data_index], 2)
82
+ data_index += 1
83
+ if data_index < data_len:
84
+ pixel[2] = int(b[:-1] + binary_secret_data[data_index], 2)
85
+ data_index += 1
86
+ if data_index >= data_len:
87
+ break
88
+ return image
89
+ def conv_im(qr_link,data):
90
+ uniqnum = uniq.uuid4()
91
+
92
+ byte_size = len(data)
93
+ print (f'bytes:{byte_size}')
94
+ data_pixels = byte_size*4
95
+ print (f'pixels:{data_pixels}')
96
+ #data_sq=data_pixels/2
97
+ data_sq = int(math.sqrt(data_pixels))
98
+ data_pad = data_sq+100
99
+ print (f'square image:{data_pad}x{data_pad}')
100
+
101
+ qr_im = make_qr(txt=qr_link)
102
+ img1 = Image.open(qr_im)
103
+ imgw = img1.size[0]
104
+ imgh = img1.size[1]
105
+ print (f'qr Size:{img1.size}')
106
+ #img1.thumbnail((imgw*4,imgh*4), Image.Resampling.LANCZOS)
107
+ img1 = img1.resize((int(data_pad),int(data_pad)), Image.Resampling.LANCZOS)
108
+ print (img1.size)
109
+ img1.save(f'tmpim{uniqnum}.png')
110
+
111
+ with open(f'tmpim{uniqnum}.png', "rb") as image_file:
112
+ encoded_string = base64.b64encode(image_file.read())
113
+ image_file.close()
114
+ im_out = encode(f'tmpim{uniqnum}.png',data)
115
+ return im_out