forked from pytorch/serve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtobytes.py
29 lines (26 loc) · 778 Bytes
/
tobytes.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
import argparse
import base64
import json
import uuid
parser = argparse.ArgumentParser()
parser.add_argument("filename", help="converts image to bytes array", type=str)
args = parser.parse_args()
image = open(args.filename, "rb") # open binary file in read mode
image_read = image.read()
image_64_encode = base64.b64encode(image_read)
bytes_array = list(image_64_encode.decode("utf-8"))
request = {
"inputs": [
{
"name": str(uuid.uuid4()),
"shape": [-1],
"datatype": "BYTES",
"data": bytes_array,
}
]
}
result_file = "{filename}.{ext}".format(
filename=str(args.filename).split(".")[0], ext="json"
)
with open(result_file, "w") as outfile:
json.dump(request, outfile, indent=4, sort_keys=True)