Código de Python - Aplicación para ocultar información de texto en imágenes o fotografías

Imágen de perfil
Val: 712
Bronce
Ha aumentado 1 puesto en Python (en relación al último mes)
Gráfica de Python

Aplicación para ocultar información de texto en imágenes o fotografíasgráfica de visualizaciones


Python

Actualizado el 5 de Marzo del 2021 por Antonio (75 códigos) (Publicado el 24 de Febrero del 2021)
3.612 visualizaciones desde el 24 de Febrero del 2021
Aplicación para codificar o decodificar información de texto en archivos de imagen (usando la cadena "=====" como marcador) mediante la técnica del "bit menos significativo".

La imagen se selecciona mediante el botón "SEARCH".
En el modo "Encode" el texto a ocultar se introduce en el espacio superior. (el programa generará un nuevo archivo de imagen cuyo nombre tendrá el prefijo "encoded_" delante del título del archivo original.
En el modo "Decode" el texto oculto se muestra en el espacio superior.

PARA CUALQUIER DUDA U OBSERVACIÓN USEN LA SECCIÓN DE COMENTARIOS.

steg

Repositorio en GitHub:
https://github.com/antonioam82/Steganography

1.0

Actualizado el 21 de Marzo del 2021 (Publicado el 24 de Febrero del 2021)gráfica de visualizaciones de la versión: 1.0
3.613 visualizaciones desde el 24 de Febrero del 2021
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

Lenguaje: Python
Librerías y recursos: Tkinter, opencv, numpy, threading, os
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from tkinter import *
from tkinter import ttk
from tkinter import messagebox, filedialog
import tkinter.scrolledtext as sct
import threading
import re
import cv2
import os
import numpy as np
 
class app():
    def __init__(self):
        self.window = Tk()
        self.window.title("Image Steganography")
        self.window.geometry("593x402")
        self.backgr = "gray90"
 
        self.imaname = StringVar()
        self.current_dir = StringVar()
        self.mode = StringVar()
        self.mode.set("EN")
        self.nbytes = IntVar()
        self.file_name = ""
        self.running = False
 
        self.entryDir = Entry(self.window,width=98,textvariable=self.current_dir)
        self.entryDir.place(x=0,y=0)
        self.textEntry = sct.ScrolledText(self.window,width=70,height=15)
        self.textEntry.place(x=5,y=28)
        self.btnClear = Button(self.window,text="CLEAR TEXT",width=20,bg=self.backgr,command=self.clear)
        self.btnClear.place(x=5,y=277)
        self.rdbEncode = Radiobutton(self.window,text="Encode",variable=self.mode,value="EN",command=self.set_mode)
        self.rdbEncode.place(x=420,y=277)
        self.rdbDecode = Radiobutton(self.window,text="Decode",variable=self.mode,value="DE",command=self.set_mode)
        self.rdbDecode.place(x=506,y=277)
        self.btnSearch = Button(self.window,text="SEARCH",width=20,bg=self.backgr,command=self.open_file)
        self.btnSearch.place(x=5,y=315)
        self.entImage = Entry(self.window,width=37,font=('arial',14),textvariable=self.imaname)
        self.entImage.place(x=167,y=315)
        self.btnStart = Button(self.window,text="START ENCODING",width=81,bg=self.backgr,command=self.init_task)
        self.btnStart.place(x=5,y=353)
        self.bylab = Label(self.window,text="BYTES AVAILABLE:")
        self.bylab.place(x=167,y=280)
        self.byEnt = Entry(self.window,textvariable=self.nbytes,width=17)
        self.byEnt.place(x=275,y=281)
        self.invLabel = Label(self.window,text="",fg="blue",width=83)
        self.invLabel.place(x=3,y=380)
 
 
        self.show_dir()
 
        self.window.mainloop()
 
    def show_dir(self):
        self.current_dir.set(os.getcwd())
 
    def set_mode(self):
        self.btnStart.configure(text="START {}CODING".format(self.mode.get()))
 
    def open_file(self):
        file = filedialog.askopenfilename(initialdir="/",title="SELECT FILE",
               filetypes =(("PNG files","*.PNG") ,("TIFF files","*.TIFF")))
        if file != "":
            self.file_name = file.split("/")[-1]
            os.chdir(("/").join(file.split("/")[:-1]))
            self.show_dir()
            self.imaname.set(self.file_name)
            try:
                self.image = cv2.imread(file)
                self.n_bytes = self.image.shape[0] * self.image.shape[1] * 3 // 8
                self.nbytes.set(self.n_bytes)
            except:
                messagebox.showwarning("ERROR","Can't open the file.")
 
    def clear(self):
        self.textEntry.delete('1.0',END)
 
    def to_bin(self,data):
        if isinstance(data, str):
            return ''.join([ format(ord(i), "08b") for i in data ])
        elif isinstance(data, bytes) or 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 encode(self):
        #secret_data = self.textEntry.get('1.0',END)
        secret_data = re.sub("\[\d+\]","",self.textEntry.get('1.0',END))
        if len(secret_data) <= self.n_bytes:
            secret_data += "====="
            data_index = 0
            binary_secret_data = self.to_bin(secret_data)
            data_len = len(binary_secret_data)
            for row in self.image:
                for pixel in row:
                    r, g, b = self.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
            ima_name = "encoded_"+self.file_name
            cv2.imwrite(ima_name,self.image)
            messagebox.showinfo("TASK COMPLETED","Created image: {}".format(ima_name))
        else:
            messagebox.showwarning("ERROR","Insufficient bytes, need bigger image or less data.")
        self.invLabel.configure(text="")
        self.running = False
 
    def decode(self):
        binary_data = ""
        for row in self.image:
            for pixel in row:
                r, g, b = self.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
        self.clear()
        if "=====" in decoded_data:
            self.clear()
            self.textEntry.insert(END,decoded_data[:-5])
        else:
            messagebox.showwarning("NO DATA","No data encoded.")
        self.invLabel.configure(text="")
        self.running = False
 
    def init_task(self):
        if self.file_name != "":
            if self.mode.get()=="EN" and len(self.textEntry.get('1.0',END))>1 and self.running==False:
                self.running = True
                self.invLabel.configure(text="ENCODING...")
                t = threading.Thread(target=self.encode)
                t.start()
            elif self.mode.get()=="DE" and self.running==False:
                self.running = True
                self.invLabel.configure(text="DECODING...")
                t = threading.Thread(target=self.decode)
                t.start()
        else:
            messagebox.showwarning("NO FILE","Select image file.")
 
 
if __name__=="__main__":
    app()



Comentarios sobre la versión: 1.0 (0)


No hay comentarios
 

Comentar la versión: 1.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s6904