-
Notifications
You must be signed in to change notification settings - Fork 501
Expand file tree
/
Copy pathimage_encoder.py
More file actions
53 lines (43 loc) · 1.47 KB
/
image_encoder.py
File metadata and controls
53 lines (43 loc) · 1.47 KB
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
__author__ = "Sri Manikanta Palakollu"
__date__ = "08-07-2020"
import argparse
import base64
import json
import time
def b64_encode(source_filepath):
with open(source_filepath, "rb") as f:
data = f.read()
dest = open("ImageData/encodeData.json", "r")
flag = json.loads(dest.read())
key = (str(int(time.time()))).decode("utf-8")
d = {
"data": base64.b64encode(data).decode("utf-8"),
"ext": source_filepath[source_filepath.index(".") :],
}
flag[key] = d
dest.close()
dest = open("ImageData/encodeData.json", "w")
json.dump(flag, dest)
return key
def b64_decode(key, dest_path):
source = open("ImageData/encodeData.json", "r")
flag = json.loads(source.read())
name = key + str(flag[key]["ext"])
dest = open(dest_path + name, "wb")
dest.write(base64.b64decode((flag[key]["data"]).encode("utf-8")))
dest.close()
return dest_path + name
parser = argparse.ArgumentParser()
parser.add_argument(
"-ie", "--ImageEncode", required=False, help="Image File for Encoding Purpose."
)
parser.add_argument("-id", "--ImageDecode", required=False, help="Image File Decoding")
parser.add_argument("-k", "--Key", required=False, help="Key for Decoding the Image")
args = vars(parser.parse_args())
try:
if args["ImageEncode"]:
print("Key Value: {}".format(b64_encode(args["ImageEncode"])))
else:
b64_decode(args["Key"], args["ImageDecode"])
except Exception:
print("Something went Wrong..!")