-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimg_proc.py
50 lines (42 loc) · 1.34 KB
/
img_proc.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
import json
import time, base64
class img_pre_proc():
def __init__(self, encoding, compression, device_id):
self.encoding = encoding
self.compression = compression
self.device_id = device_id
def read_raw_img (self,path):
in_fh = open(path, "rb") #get raw byte data
raw_bytes = in_fh.read()
in_fh.close()
return raw_bytes
def encode_b64(self, raw_bytes):
return str( base64.b64encode(raw_bytes) )
def json_for_img(self, encoded_str):
data = {}
data['id'] = self.device_id
data['ts'] = time.time_ns()
data['compr'] = self.compression
data['enc'] = self.encoding
data['data'] = encoded_str
return json.dumps(data)
def preproc_and_get_json(self,path):
raw_bytes = self.read_raw_img(path)
encoded_str = self.encode_b64(raw_bytes)
# with open('b64.txt','w') as fh:
# fh.write(encoded_str)
json_object = self.json_for_img(encoded_str)
return json_object
# for debug purposes only
def main():
# path = input("Enter absolute file path to read ")
path = "./sample-data/test-img.raw"
img_processor = img_pre_proc(encoding='b64',compression='')
json_obj = img_processor.preproc_and_get_json(path= path)
print(json_obj)
# with open("./ignore/tmp.bin","wb") as out_file:
# out_file.write(raw_bytes)
# with open("./ignore/b64.txt","w") as out_file:
# out_file.write(encoded_str)
if __name__ == "__main__":
main()