-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.py
More file actions
39 lines (26 loc) · 836 Bytes
/
decode.py
File metadata and controls
39 lines (26 loc) · 836 Bytes
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
from PIL import Image
import binascii
def binary_to_chr(binary):
string = int(binary, 2)
string = chr(string)
return string
def chunks(l, n):
n = max(1, n)
return (l[i:i+n] for i in range(0, len(l), n))
def decode(pixels):
code_in_binary = []
for x in range(0,len(pixels)):
bit_num = 0
for bit in pixels[x]:
if pixels[x][bit_num] % 2 == 0:
code_in_binary.append('0')
elif pixels[x][bit_num] % 2 == 1:
code_in_binary.append('1')
bit_num += 1
code_in_binary = "".join(code_in_binary)
chunked_message = list(chunks(code_in_binary, 8))
message = []
for byte in chunked_message:
message.append(binary_to_chr(byte))
message = "".join(message)
return(message.split('~')[0])