-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathencode_downsampled.py
More file actions
51 lines (39 loc) · 1.84 KB
/
Copy pathencode_downsampled.py
File metadata and controls
51 lines (39 loc) · 1.84 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
# Copyright 2023-2025 XMOS LIMITED.
# This Software is subject to the terms of the XMOS Public Licence: Version 1.
import argparse
import numpy as np
from PIL import Image
def encode_downsampled_image(input_name=None, width=160, height=120, img_dtype="uint8", output_name=None):
# this takes an image in png, jpg, etc using pillow
# then it propduce a binary file with the image
# the image size is width x height
# the image is in RGB format
# the image is in img_dtype format
img = Image.open(input_name).convert('RGB')
img = img.resize((width, height))
img = np.array(img)
if img_dtype == "uint8":
buffer = img.tobytes()
elif img_dtype == "int8":
buffer = img.astype(np.int32) - 128 # avoid overflow
buffer = buffer.astype(np.int8)
buffer = buffer.tobytes()
else:
raise ValueError("dtype not supported")
if output_name is None:
output_name = input_name.split(".")[0] + img_dtype + ".bin"
with open(output_name, "wb") as f:
f.write(buffer)
# print a complete message
print(f"Image {input_name} was downsampled to {output_name} with size {width}x{height} and dtype {img_dtype}")
print(f"Buffer len: {len(buffer)}")
if __name__ == "__main__":
argparse = argparse.ArgumentParser()
argparse.add_argument("--input", help="input file name", default="capture.bin")
argparse.add_argument("--width", help="image width", default=160, type=int)
argparse.add_argument("--height", help="image height", default=120, type=int)
argparse.add_argument("--dtype", help="image dtype", default="uint8")
argparse.add_argument("--output", help="image output file name", default=None)
args = argparse.parse_args()
encode_downsampled_image(args.input, args.width, args.height, args.dtype, args.output)
print("done")