-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtask6.py
More file actions
74 lines (56 loc) · 1.71 KB
/
task6.py
File metadata and controls
74 lines (56 loc) · 1.71 KB
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
import random
from PIL import Image, ImageDraw
from bitarray._bitarray import bitarray
from bitarray.util import int2ba
text = b'Very very secret text'
lamb = 0.7
sigma = 3
K0 = 42
image = Image.open("temp.jpg")
draw = ImageDraw.Draw(image)
width = image.size[0]
height = image.size[1]
pix = image.load()
bit_chars = []
for char in text:
bites = [1 if i else 0 for i in int2ba(char)]
bit_chars.append(([0] * (8 - len(bites)) + bites))
random.seed(K0)
for i in range(len(bit_chars)):
for j in range(len(bit_chars[i])):
count = i * len(bit_chars) + j
x = random.uniform(sigma, width - sigma)
y = random.uniform(sigma, height - sigma)
r = pix[x, y][0]
g = pix[x, y][1]
b = pix[x, y][2]
power = 0.3 * r + 0.59 * g + 0.11 * b
b += int((2 * bit_chars[i][j] - 1) * lamb * power)
if b < 0:
b = 0
if b > 255:
b = 255
draw.point((x, y), (r, g, b))
image.save("temp_with_secret.png", "PNG")
print('Скрытие информации завершено')
image = Image.open("temp_with_secret.png")
pix = image.load()
random.seed(K0)
width = image.size[0]
height = image.size[1]
res = bitarray()
for i in range(sigma, width - sigma):
for j in range(sigma, height - sigma):
x = random.uniform(sigma, width - sigma)
y = random.uniform(sigma, height - sigma)
temp_b = 0
b = pix[x, y][2]
for k in range(1, sigma + 1):
bt = pix[x, y + k][2]
bd = pix[x, y - k][2]
bl = pix[x - k, y][2]
br = pix[x, y + k][2]
temp_b += bt + bd + bl + br
temp_b /= 4 * sigma
res += [temp_b < b]
print(res.tobytes()[:len(text)])