-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageMessage.py
96 lines (79 loc) · 2.38 KB
/
ImageMessage.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import sys
from PIL import Image
def encode(image, msg):
im = Image.open(image)
pix = im.load()
width, height = im.size
ms = list(open(msg, "r+").read())
for i in range(0, height):
for j in range(0, width):
if (i * width) + j > len(ms) - 1:
break
c = ord(ms[(i * width) + j])
p3 = pix[j, i][2] - (pix[j, i][2] % 10) + (c % 10)
if p3 > 255:
p3 -= 10
c = int(c / 10)
p2 = pix[j, i][1] - (pix[j, i][1] % 10) + (c % 10)
if p2 > 255:
p2 -= 10
c = int(c / 10)
p1 = pix[j, i][0] - (pix[j, i][0] % 10) + c
if p1 > 255:
p1 -= 10
pix[j, i] = (p1, p2, p3)
if (i * width) + j > len(ms) - 1:
p3 = pix[j, i][2] - (pix[j, i][2] % 10)
p2 = pix[j, i][1] - (pix[j, i][1] % 10)
p1 = pix[j, i][0] - (pix[j, i][0] % 10)
pix[j, i] = (p1, p2, p3)
break
im.save("./out.png", format="PNG", compress_level=0)
def decode(image, out):
im = Image.open(image)
pix = im.load()
width, height = im.size
msg = ""
breakFlag = False
for i in range(0, height):
for j in range(0, width):
p3 = pix[j, i][2] % 10
p2 = pix[j, i][1] % 10
p1 = pix[j, i][0] % 10
val = p1 * 100 + p2 * 10 + p3
if val == 0:
breakFlag = True
break
try:
c = chr(val)
except ValueError:
c = ""
msg += c
if breakFlag:
break
with open(out, "w") as f:
f.write(msg)
def main():
try:
flag = sys.argv[1]
image = sys.argv[2]
file = sys.argv[3]
except IndexError:
print("Invalid args. Correct usage:")
print(" ImageMessage -[e,d] <imagename> <in/output filename>")
return -1
if flag == "-e":
try:
encode(image, file)
except AttributeError:
print("Invalid image name")
elif flag == "-d":
try:
decode(image, file)
except AttributeError:
print("Invalid image name")
else:
print("Invalid args. Correct usage:")
print(" ImageMessage -[e,d] <imagename> <in/output filename>")
if __name__ == "__main__":
main()