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
| from PIL import Image
def questions_nom_imgs () :
"""
asks for the name of the visible image and the secret image and sends them back
"""
variable_1 = input("Quel est le nom de l'image visible?")
variable_2 = input("Quel est le nom de l'image secrete?")
return (variable_1, variable_2)
def importer_images(nom_img_1 : str, nom_img_2 : str):
"""
import the visible and secret image and store them in variables that are returned
"""
img_visible = Image.open(nom_img_1+'.jpg')
img_secrete = Image.open(nom_img_2+'.jpg')
return (img_visible,img_secrete)
def dimensions(image_1 ,image_2):
"""
checks that the dimensions of the two images are the same, returns "True" if they are the same
"""
(l1,h1) = image_1.size
(l2, h2) = image_2.size
if l1 == l2 and h1 == h2:
return True
else:
return False
def conversion_binaire_8_bits(image,x : int ,y : int):
"""
takes the colour in decimal of one pixel of (x,y) coordinates of the image
and converts colour to 8-bit binary
"""
couleur_decimal = image.getpixel((x,y))
couleur_binaire = []
for i in range (8):
reste = couleur_decimal % 2
couleur_binaire.insert(0,reste)
couleur_decimal = couleur_decimal //2
return (couleur_binaire)
def garder_4_bits(binaire):
"""
keep the 4 bits first out of the 8 bits
"""
quatre_bits = []
for i in range (4) :
quatre_bits.append(binaire[i])
return (quatre_bits)
def garder_4_bits_2(binaire):
"""
keep the 4 bits first out of the 8 bits
"""
quatre_bits = []
for i in range (4,7) :
quatre_bits.append(binaire[i])
return (quatre_bits)
def addition_et_conversion (quatre_bits_vis,quatre_bits_sec):
"""
add the bits and convert them to decimal
"""
couleur_binaire = quatre_bits_vis + quatre_bits_sec
couleur_decimal = 0
cpt_parcour = 7
for i in range (8):
if couleur_binaire[cpt_parcour] == 1:
couleur_decimal = couleur_decimal + 2**i
cpt_parcour = cpt_parcour - 1
return couleur_decimal
def steganographie():
"""
hides one image in the other under the condition that it is the same size
"""
(nom_img_visible, nom_img_secrete) = questions_nom_imgs()
(img_sec, img_vis) = importer_images(nom_img_visible, nom_img_secrete)
same_dim = dimensions(img_sec, img_vis)
if same_dim :
largeur, hauteur = (img_vis.size)
nvlle_img = Image.new('L',(largeur,hauteur)) #création d'une nouvelle image de la taille de image visible
for x in range(largeur) :
for y in range(hauteur) :
huits_bits_vis = conversion_binaire_8_bits(img_vis,x,y)
quatre_bits_vis = garder_4_bits(huits_bits_vis)
huits_bits_sec = conversion_binaire_8_bits(img_sec,x,y)
quatre_bits_sec = garder_4_bits(huits_bits_sec)
couleur = addition_et_conversion(quatre_bits_vis, quatre_bits_sec)
nvlle_img.putpixel((x, y), couleur)
return nvlle_img.save('steganographie.jpg')
else:
return("Les deux images n'ont pas la même dimension.")
def decode():
img_a_decoder = Image.open('steganographie.jpg')
largeur, hauteur = (img_a_decoder.size)
image_cachee = Image.new('L',(largeur,hauteur))
for x in range(largeur):
for y in range(hauteur):
couleur_huits_bits = conversion_binaire_8_bits(img_a_decoder,x,y)
couleur_quatres_bits = garder_4_bits_2(couleur_huits_bits)
zero = [0,0,0,0]
couleur_decimal = addition_et_conversion(couleur_quatres_bits, zero)
image_cachee.putpixel((x, y), couleur_decimal)
return image_cachee.save('image_cachée.jpg') |
Partager