-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryhelper.py
More file actions
33 lines (26 loc) · 878 Bytes
/
Copy pathbinaryhelper.py
File metadata and controls
33 lines (26 loc) · 878 Bytes
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
import base64
import json
def file_to_json(filename, meta={}):
result = dict(meta)
with open(filename, "rb") as file:
data = file.read()
result["data"] = base64.b64encode(data)
result["filename"] = filename
return json.dumps(result)
def json_to_file(file_json, new_file_name=None):
json_dict = json.loads(file_json)
dict_to_file(json_dict,new_file_name)
def dict_to_file(file_dict, new_file_name=None):
if file_dict.get("data",None) == None:
print "No data in json!"
return
data = base64.b64decode(file_dict["data"])
if new_file_name is not None:
fileName = new_file_name
else:
fileName = file_dict["filename"]
with open(fileName, "wb+") as file:
file.write(data)
if __name__ == '__main__':
buddy = file_to_json("image.jpg")
json_to_file(buddy, "image2.jpg")