import base64 import gradio as gr import cv2 import numpy as np import pandas as pd from PIL import Image import hashlib 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) #resultd = hashlib.sha256(txt.encode('utf-8')).hexdigest() 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 p = decoded_data[:-5].split("#",1)[1].split("#",1)[0] #if p == resultd: this = decoded_data[:-5].split("#",1)[0].split("'",1)[1] #base = bytes(this, 'utf-8') #with open(f"finished_im{uniqnum}.png", "wb") as fh: # fh.write(base64.decodebytes(bytes(this, 'utf-8'))) #fh.close #return f"finished_im{uniqnum}.png" 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) #resultp = hashlib.sha256(txt.encode('utf-8')).hexdigest() secret_data1=secret_data #secret_data1=f'{secret_data}#{resultp}' while True: if len(secret_data1) < (n_bytes): secret_data1 = f'{secret_data1}#' elif len(secret_data1) >= (n_bytes): break secret_data = secret_data1 if len(secret_data) > n_bytes: return image_name, gr.Markdown.update("""