-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecoder.py
44 lines (38 loc) · 1.01 KB
/
Decoder.py
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
from PIL import Image
import bitarray
endMessage = "11111111"
img = Image.open("encoderImage.png")
width, height = img.size
x = 0
y = 0
bits = 0
bitsArray = []
bytesArray = []
while True:
if(bits != 8):
coordinate = (x,y)
try:
[r, g, b, a] = img.getpixel(coordinate)
except: #incase image does not have alpha
[r, g, b] = img.getpixel(coordinate)
a = 255
lastR = list(str(r))
lastR = lastR[len(lastR)-1]
bitsArray.append(lastR)
bits += 1
if x != width-1:
x += 1
else:
y += 1
x = 0
else:
byte = ''.join(bitsArray)
if byte == endMessage:
break
else:
bytesArray.append(byte)
bits = 0
bitsArray = []
allBitArrayStr = ''.join(bytesArray)
messageDecoded = bitarray.bitarray(allBitArrayStr).tobytes().decode('utf-8')
print("Decoded Message: " + messageDecoded)