-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncoder.py
67 lines (52 loc) · 1.72 KB
/
Encoder.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from PIL import Image
import bitarray
endMessage = "11111111"
img = Image.open("encoderImage.png")
message = input("What text would you like to encode? ")
width, height = img.size
messageBitArray = bitarray.bitarray()
messageBitArray.frombytes(message.encode('utf-8'))
messageBitArray = list(messageBitArray)
x = 0
y = 0
for bit in messageBitArray:
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
#Turn R into r, but remove last pixel and turn it into the bit we are encoding
RIntString = list(str(r))
#Only change the very last number of the r string to make a minimal change
RIntString[len(RIntString)-1] = str(bit)
newRInt = ""
newRInt = ''.join(RIntString)
img.putpixel((coordinate), (int(newRInt), g, b, a))
if x != width-1:
x += 1
else:
y += 1
x = 0
for bit in endMessage:
coordinate = (x,y)
#get pixel values
try:
[r, g, b, a] = img.getpixel(coordinate)
except: #incase image does not have alpha
[r, g, b] = img.getpixel(coordinate)
a = 255
#Turn R into r, but remove last pixel and turn it into the bit we are encoding
RIntString = list(str(r))
#Only change the very last number of the r string to make a minimal change
RIntString[len(RIntString)-1] = str(bit)
newRInt = ""
newRInt = ''.join(RIntString)
img.putpixel((coordinate), (int(newRInt), g, b, a))
if x != width:
x += 1
else:
y += 1
x = 0
img.save("encoderImage.png")
print("Text successfully encoded into image!")