Improved code style.

This commit is contained in:
Cédric Bonhomme 2019-12-17 09:18:37 +01:00
parent 9b216d9d59
commit 71f6c08c28
No known key found for this signature in database
GPG key ID: A1CB94DE57B7A70D
19 changed files with 335 additions and 253 deletions

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#-*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
from .exifHeader import *

View file

@ -30,11 +30,7 @@ from stegano import tools
def hide(
input_image_file,
img_enc,
secret_message=None,
secret_file=None,
img_format=None,
input_image_file, img_enc, secret_message=None, secret_file=None, img_format=None,
):
"""Hide a message (string) in an image.
"""

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#-*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
from .lsb import *

View file

@ -47,9 +47,7 @@ def hide(
if img.mode not in ["RGB", "RGBA"]:
if not auto_convert_rgb:
print(
"The mode of the image is not RGB. Mode is {}".format(img.mode)
)
print("The mode of the image is not RGB. Mode is {}".format(img.mode))
answer = input("Convert the image to RGB ? [Y / n]\n") or "Y"
if answer.lower() == "n":
raise Exception("Not a RGB image.")
@ -67,9 +65,7 @@ def hide(
len_message_bits = len(message_bits)
if len_message_bits > npixels * 3:
raise Exception(
"The message you want to hide is too long: {}".format(
message_length
)
"The message you want to hide is too long: {}".format(message_length)
)
for row in range(height):
for col in range(width):
@ -101,10 +97,7 @@ def hide(
return encoded
def reveal(input_image: Union[str, IO[bytes]],
encoding: str = "UTF-8",
shift: int = 0
):
def reveal(input_image: Union[str, IO[bytes]], encoding: str = "UTF-8", shift: int = 0):
"""Find a message in an image (with the LSB technique).
"""
img = tools.open_image(input_image)

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#-*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
from .lsbset import *

View file

@ -175,36 +175,38 @@ def log_gen() -> Iterator[int]:
y = y + int(adder)
polys = {2: [2, 1],
3: [3, 1],
4: [4, 1],
5: [5, 2],
6: [6, 1],
7: [7, 1],
8: [8, 4, 3, 2],
9: [9, 4],
10: [10, 3],
11: [11, 2],
12: [12, 6, 4, 1],
13: [13, 4, 3, 1],
14: [14, 8, 6, 1],
15: [15, 1],
16: [16, 12, 3, 1],
17: [17, 3],
18: [18, 7],
19: [19, 5, 2, 1],
20: [20, 3],
21: [21, 2],
22: [22, 1],
23: [23, 5],
24: [24, 7, 2, 1],
25: [25, 3],
26: [26, 6, 2, 1],
27: [27, 5, 2, 1],
28: [28, 3],
29: [29, 2],
30: [30, 23, 2, 1],
31: [31, 3]}
polys = {
2: [2, 1],
3: [3, 1],
4: [4, 1],
5: [5, 2],
6: [6, 1],
7: [7, 1],
8: [8, 4, 3, 2],
9: [9, 4],
10: [10, 3],
11: [11, 2],
12: [12, 6, 4, 1],
13: [13, 4, 3, 1],
14: [14, 8, 6, 1],
15: [15, 1],
16: [16, 12, 3, 1],
17: [17, 3],
18: [18, 7],
19: [19, 5, 2, 1],
20: [20, 3],
21: [21, 2],
22: [22, 1],
23: [23, 5],
24: [24, 7, 2, 1],
25: [25, 3],
26: [26, 6, 2, 1],
27: [27, 5, 2, 1],
28: [28, 3],
29: [29, 2],
30: [30, 23, 2, 1],
31: [31, 3],
}
def LFSR(m: int) -> Iterator[int]:
@ -227,5 +229,5 @@ def LFSR(m: int) -> Iterator[int]:
# Add the feedback bit
state.insert(0, feedback)
# Convert the registers to an int
out = sum([e * (2**i) for i, e in enumerate(state)])
out = sum([e * (2 ** i) for i, e in enumerate(state)])
yield out

View file

@ -50,9 +50,7 @@ def hide(
if img.mode not in ["RGB", "RGBA"]:
if not auto_convert_rgb:
print(
"The mode of the image is not RGB. Mode is {}".format(img.mode)
)
print("The mode of the image is not RGB. Mode is {}".format(img.mode))
answer = input("Convert the image to RGB ? [Y / n]\n") or "Y"
if answer.lower() == "n":
raise Exception("Not a RGB image.")
@ -70,9 +68,7 @@ def hide(
len_message_bits = len(message_bits)
if len_message_bits > npixels * 3:
raise Exception(
"The message you want to hide is too long: {}".format(
message_length
)
"The message you want to hide is too long: {}".format(message_length)
)
while shift != 0:
next(generator)
@ -126,7 +122,7 @@ def reveal(
while True:
generated_number = next(generator)
# color = [r, g, b]
for color in img_list[generated_number][:3]: # ignore the alpha
for color in img_list[generated_number][:3]: # ignore the alpha
buff += (color & 1) << (tools.ENCODINGS[encoding] - 1 - count)
count += 1
if count == tools.ENCODINGS[encoding]:

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python
#-*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
from .red import *

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
#-*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
from .parity import *
from .statistics import *

View file

@ -32,26 +32,28 @@ from PIL import Image
from collections import Counter
from collections import OrderedDict
def steganalyse(img):
"""
Steganlysis of the LSB technique.
"""
encoded = img.copy()
width, height = img.size
colours_counter = Counter() # type: typing.Counter[int]
colours_counter = Counter() # type: typing.Counter[int]
for row in range(height):
for col in range(width):
r, g, b = img.getpixel((col, row))
colours_counter[r] += 1
most_common = colours_counter.most_common(10)
dict_colours = OrderedDict(sorted(list(colours_counter.items()),
key=lambda t: t[1]))
dict_colours = OrderedDict(
sorted(list(colours_counter.items()), key=lambda t: t[1])
)
colours = 0 # type: float
colours = 0 # type: float
for colour in list(dict_colours.keys()):
colours += colour
colours = colours / len(dict_colours)
#return colours.most_common(10)
# return colours.most_common(10)
return list(dict_colours.keys())[:30], most_common

View file

@ -41,9 +41,7 @@ def a2bits(chars: str) -> str:
>>> a2bits("Hello World!")
'010010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001'
"""
return bin(reduce(lambda x, y: (x << 8) + y, (ord(c) for c in chars), 1))[
3:
]
return bin(reduce(lambda x, y: (x << 8) + y, (ord(c) for c in chars), 1))[3:]
def a2bits_list(chars: str, encoding: str = "UTF-8") -> List[str]: