mirror of
https://github.com/BreizhHardware/py_A2.git
synced 2026-01-18 16:47:38 +01:00
Encoding Text with delimiter work, but the other don't work
This commit is contained in:
BIN
TP2/Gladius.jpg
BIN
TP2/Gladius.jpg
Binary file not shown.
|
Before Width: | Height: | Size: 646 KiB |
BIN
TP2/Gladius.png
Normal file
BIN
TP2/Gladius.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 5.2 MiB |
Binary file not shown.
|
Before Width: | Height: | Size: 9.6 KiB |
BIN
TP2/Zeus-MKII-CL.png
Normal file
BIN
TP2/Zeus-MKII-CL.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 91 KiB |
13
TP2/main.py
13
TP2/main.py
@@ -57,6 +57,10 @@ def __tp2_optionnel__():
|
||||
|
||||
|
||||
def __tp_steganographie_text__():
|
||||
hiddenText1 = Image.open("hiddenText1.png")
|
||||
hiddenText2 = Image.open("hiddenText2.png")
|
||||
hiddenText3 = Image.open("hiddenText3.png")
|
||||
hiddenText4 = Image.open("hiddenText4.png")
|
||||
print(get_hidden_text_with_delimiter(hiddenText1, "\0"))
|
||||
print(get_hidden_text_with_delimiter(hiddenText2, "\0"))
|
||||
print(get_hidden_text_of_length(hiddenText3, 4))
|
||||
@@ -64,6 +68,10 @@ def __tp_steganographie_text__():
|
||||
|
||||
|
||||
def __tp_steganographie_image__():
|
||||
hiddenImage1 = Image.open("hiddenImage1.png")
|
||||
hiddenImage2 = Image.open("hiddenImage2.png")
|
||||
hiddenImage3 = Image.open("hiddenImage3.png")
|
||||
hiddenImage4 = Image.open("hiddenImage4.png")
|
||||
hidden1 = get_hidden_image(hiddenImage1, 8)
|
||||
hidden1.show()
|
||||
hidden2 = get_hidden_image(hiddenImage2, 8)
|
||||
@@ -75,13 +83,16 @@ def __tp_steganographie_image__():
|
||||
|
||||
|
||||
def __tp_steganographie_hide__():
|
||||
gladius = Image.open("Gladius.png")
|
||||
zeusMK2CL = Image.open("Zeus-MKII-CL.png")
|
||||
small = Image.open("small.png")
|
||||
gladius_with_hidden_text_delimiter = set_hidden_text_with_delimiter(gladius, "Vive Star Citizen!", "\0")
|
||||
gladius_decoded_delimiter = get_hidden_text_with_delimiter(gladius_with_hidden_text_delimiter, "\0")
|
||||
print(gladius_decoded_delimiter)
|
||||
zeus_with_hidden_text_length = set_hidden_text_of_length(zeusMK2CL, "I Held the line !", 4)
|
||||
zeus_decoded_length = get_hidden_text_of_length(zeus_with_hidden_text_length, 4)
|
||||
print(zeus_decoded_length)
|
||||
gladius_with_hidden_image = set_hidden_image(gladius, zeusMK2CL, 8)
|
||||
gladius_with_hidden_image = set_hidden_image(gladius, small, 8)
|
||||
gladius_with_hidden_image.show()
|
||||
gladius_decoded_image = get_hidden_image(gladius_with_hidden_image, 8)
|
||||
gladius_decoded_image.show()
|
||||
|
||||
BIN
TP2/small.png
Normal file
BIN
TP2/small.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
@@ -1,16 +1,5 @@
|
||||
from PIL import Image
|
||||
|
||||
hiddenText1 = Image.open("hiddenText1.png")
|
||||
hiddenText2 = Image.open("hiddenText2.png")
|
||||
hiddenText3 = Image.open("hiddenText3.png")
|
||||
hiddenText4 = Image.open("hiddenText4.png")
|
||||
hiddenImage1 = Image.open("hiddenImage1.png")
|
||||
hiddenImage2 = Image.open("hiddenImage2.png")
|
||||
hiddenImage3 = Image.open("hiddenImage3.png")
|
||||
hiddenImage4 = Image.open("hiddenImage4.png")
|
||||
gladius = Image.open("Gladius.jpg")
|
||||
zeusMK2CL = Image.open("Zeus MKII CL.jpeg")
|
||||
|
||||
|
||||
def image_to_array(image):
|
||||
array = []
|
||||
@@ -110,9 +99,11 @@ def set_hidden_text_with_delimiter(image, text, delimiter):
|
||||
for i in range(len(text)):
|
||||
char = ord(text[i])
|
||||
for j in range(7):
|
||||
image_array[i * 7 + j] = (image_array[i * 7 + j] & 0b11111110) | ((char >> (6 - j)) & 1)
|
||||
image_array[i * 7 + j] = ((image_array[i * 7 + j] & 0b11111110) |
|
||||
((char >> (6 - j)) & 1))
|
||||
for j in range(7):
|
||||
image_array[len(text) * 7 + j] = (image_array[len(text) * 7 + j] & 0b11111110) | (ord(delimiter) >> (6 - j) & 1)
|
||||
image_array[len(text) * 7 + j] = ((image_array[len(text) * 7 + j] & 0b11111110) |
|
||||
(ord(delimiter) >> (6 - j) & 1))
|
||||
image.putdata([(image_array[i], image_array[i + 1], image_array[i + 2]) for i in range(0, len(image_array), 3)])
|
||||
return image
|
||||
|
||||
@@ -123,26 +114,35 @@ def set_hidden_text_of_length(image, text, nb_bits_for_length):
|
||||
for i in range(nb_bits_for_length):
|
||||
image_array[i] = (image_array[i] & 0b11111110) | ((length >> (nb_bits_for_length - 1 - i)) & 1)
|
||||
for i in range(length * 7):
|
||||
char = ord(text[i // 7])
|
||||
image_array[i + nb_bits_for_length] = (image_array[i + nb_bits_for_length] & 0b11111110) | ((char >> (6 - (i % 7))) & 1)
|
||||
char_index = ord(text[i // 7])
|
||||
char_offset = i % 7
|
||||
if char_index < len(text):
|
||||
char = ord(text[char_index])
|
||||
image_array[i + nb_bits_for_length] = ((image_array[i + nb_bits_for_length] & 0b11111110) |
|
||||
((char >> (6 - char_offset)) & 1))
|
||||
else:
|
||||
image_array[i + nb_bits_for_length] = (image_array[i + nb_bits_for_length] & 0b11111110)
|
||||
image.putdata([(image_array[i], image_array[i + 1], image_array[i + 2]) for i in range(0, len(image_array), 3)])
|
||||
return image
|
||||
|
||||
|
||||
def set_hidden_image(image_original, image_to_hide, nb_bits_for_size, nb_last_bits = 1):
|
||||
list_bit = get_list_last_k_bits(image_original, nb_last_bits)
|
||||
bin = ""
|
||||
binary_data = ""
|
||||
width, height = image_to_hide.size
|
||||
for i in range(nb_bits_for_size // nb_last_bits):
|
||||
bin += '{0:08b}'.format(width)[i * nb_last_bits:(i + 1) * nb_last_bits:]
|
||||
bin += '{0:08b}'.format(height + 1)[i * nb_last_bits:(i + 1) * nb_last_bits:]
|
||||
binary_data += '{0:08b}'.format(width, f'0{nb_last_bits}b')
|
||||
binary_data += '{0:08b}'.format(height, f'0{nb_last_bits}b')
|
||||
for i in range(height):
|
||||
for j in range(width):
|
||||
r, g, b = image_to_hide.getpixel((j, i))
|
||||
bin += '{0:08b}'.format(r)
|
||||
bin += '{0:08b}'.format(g)
|
||||
bin += '{0:08b}'.format(b)
|
||||
for k in range(len(bin)):
|
||||
list_bit[k] = list_bit[k][:-nb_last_bits:] + bin[k]
|
||||
image_original.putdata([(int(list_bit[i], 2), int(list_bit[i + 1], 2), int(list_bit[i + 2], 2)) for i in range(0, len(list_bit), 3)])
|
||||
binary_data += '{0:08b}'.format(r)
|
||||
binary_data += '{0:08b}'.format(g)
|
||||
binary_data += '{0:08b}'.format(b)
|
||||
while len(binary_data) < len(list_bit):
|
||||
binary_data += '0'
|
||||
for k in range(len(list_bit)):
|
||||
list_bit[k] = list_bit[k][:-nb_last_bits:] + binary_data[k]
|
||||
image_original.putdata([(int(list_bit[i], 2), int(list_bit[i + 1], 2), int(list_bit[i + 2], 2))
|
||||
for i in range(0, len(list_bit), 3)])
|
||||
return image_original
|
||||
|
||||
Reference in New Issue
Block a user