-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
78 lines (64 loc) · 2.77 KB
/
main.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
import sys, getopt
from image_man import check_image_mode
from text_man import *
from encoder import init_encode
from decoder import init_decode
argumentList = sys.argv[1:]
options = "hc:f:i:d:"
long_options = ["help", "code", "file =", "image =", "decode ="]
#Variables
mode = -1 #0: code 1: file 2: decode
text = ''
filename = ''
imagesrc = ''
if __name__ == "__main__":
try:
# Parsing argument and checking them
arguments, values = getopt.getopt(argumentList, options, long_options)
for currentArgument, currentValue in arguments:
if currentArgument.lower() in ("-h", "--help"):
print ("""
-h, --help: print this help message
-c, --code: the code you want to put in the image, as a string
-f, --file: text file to read the code from
-i, --image: image to put the code in
example: python3 main.py -c "Hello World!" -i image.png
or just run the program without any arguments to see the menu""")
elif currentArgument in ("-c", "--code"):
if(mode != -1): raise Exception("You can't use -c and -f at the same time")
text = currentValue
mode = 0
elif currentArgument in ("-f", "--file"):
if(mode != -1): raise Exception("You can't use -c and -f at the same time")
filename = currentValue
mode = 1
elif currentArgument in ("-i", "--image"):
imagesrc = currentValue
if not imagesrc.endswith('.png'): raise Exception("The image must be a .png file (for now)")
if not check_image_mode(imagesrc): raise Exception("This image mode is not compatible with this program")
elif currentArgument in ("-d", "--decode"):
if(mode != -1): raise Exception("You can't use -d and other arguments at the same time")
imagesrc = currentValue
mode = 2
else:
print("command not recognized, please use -h or --help for help")
except getopt.error as err:
print (str(err))
if mode == 0:
code_bytes = str_to_bytes([text])
code_bits = bytes_to_bin(code_bytes)
final_bin = ''.join(code_bits)
init_encode(final_bin, imagesrc)
elif mode == 1:
text_list = read_file(filename)
code_bytes = str_to_bytes(text_list)
code_bits = bytes_to_bin(code_bytes)
final_bin = ''.join(code_bits)
init_encode(final_bin, imagesrc)
elif mode == 2:
init_decode(imagesrc)
else:
#TODO: add menu
print("Menu should be here")
print("I didn't program this part yet, sorry")
print("use arguements to test the program or -h or --help for help")