RSA-Encryption-dev / stegan2.py
Omnibus's picture
Create stegan2.py
7a5ce4e
import base64
import cv2
import numpy as np
#import pandas as pd
from PIL import Image
import os
import uuid as uniq
from qr import make_qr
import math
def to_bin(data):
"""Convert `data` to binary format as string"""
if isinstance(data, str):
return ''.join([ format(ord(i), "08b") for i in data ])
elif isinstance(data, bytes):
return ''.join([ format(i, "08b") for i in data ])
elif isinstance(data, np.ndarray):
return [ format(i, "08b") for i in data ]
elif isinstance(data, int) or isinstance(data, np.uint8):
return format(data, "08b")
else:
raise TypeError("Type not supported.")
def decode(image_name,txt=None):
BGRimage = cv2.imread(image_name)
image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)
binary_data = ""
for row in image:
for pixel in row:
r, g, b = to_bin(pixel)
binary_data += r[-1]
binary_data += g[-1]
binary_data += b[-1]
all_bytes = [ binary_data[i: i+8] for i in range(0, len(binary_data), 8) ]
decoded_data = ""
for byte in all_bytes:
decoded_data += chr(int(byte, 2))
if decoded_data[-5:] == "=====":
break
this = decoded_data[:-5].split("#####",1)[0]
this = eval(this)
enc_in=this
return this
def encode(image_name, secret_data,txt=None):
BGRimage = cv2.imread(image_name)
image = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)
n_bytes = image.shape[0] * image.shape[1] * 3 // 8
print("[*] Maximum bytes to encode:", n_bytes)
secret_data1=secret_data
#secret_data1=f'{secret_data}#{resultp}'
while True:
if len(secret_data1)+5 < (n_bytes):
secret_data1 = f'{secret_data1}#####'
elif len(secret_data1)+5 >= (n_bytes):
break
secret_data = secret_data1
if len(secret_data) > n_bytes:
return image_name, gr.Markdown.update("""<center><h3>Input image is too large""")
secret_data += "====="
data_index = 0
binary_secret_data = to_bin(secret_data)
data_len = len(binary_secret_data)
for row in image:
for pixel in row:
r, g, b = to_bin(pixel)
if data_index < data_len:
pixel[0] = int(r[:-1] + binary_secret_data[data_index], 2)
data_index += 1
if data_index < data_len:
pixel[1] = int(g[:-1] + binary_secret_data[data_index], 2)
data_index += 1
if data_index < data_len:
pixel[2] = int(b[:-1] + binary_secret_data[data_index], 2)
data_index += 1
if data_index >= data_len:
break
return image
def conv_im(qr_link,data):
uniqnum = uniq.uuid4()
byte_size = len(data)
print (f'bytes:{byte_size}')
data_pixels = byte_size*4
print (f'pixels:{data_pixels}')
#data_sq=data_pixels/2
data_sq = int(math.sqrt(data_pixels))
data_pad = data_sq+100
print (f'square image:{data_pad}x{data_pad}')
qr_im = make_qr(txt=qr_link)
img1 = Image.open(qr_im)
imgw = img1.size[0]
imgh = img1.size[1]
print (f'qr Size:{img1.size}')
#img1.thumbnail((imgw*4,imgh*4), Image.Resampling.LANCZOS)
img1 = img1.resize((int(data_pad),int(data_pad)), Image.Resampling.LANCZOS)
print (img1.size)
img1.save(f'tmpim{uniqnum}.png')
with open(f'tmpim{uniqnum}.png', "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
image_file.close()
im_out = encode(f'tmpim{uniqnum}.png',data)
return im_out