-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_image.py
More file actions
77 lines (58 loc) · 1.81 KB
/
generate_image.py
File metadata and controls
77 lines (58 loc) · 1.81 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from os.path import isdir
import numpy as np
import imageio.v3 as imageio
import argparse
import os
import matplotlib.pyplot as plt
from pathlib import Path
CPP_SKELETON = """
#pragma once
namespace images
{{
constexpr int {array_name}_HEIGHT = {height};
constexpr int {array_name}_WIDTH = {width};
constexpr float {array_name}_RED[]
{{
{array_red}
}};
constexpr float {array_name}_GREEN[]
{{
{array_green}
}};
constexpr float {array_name}_BLUE[]
{{
{array_blue}
}};
}}
"""
def get_image_array(image: np.ndarray) -> str:
flattened = image.flatten().astype(np.float32) / 255.0
return ",".join((f"{x}f" for x in flattened))
def process_file(file_path: Path, output_folder: Path):
image = imageio.imread(str(file_path))
red = get_image_array(image[:, :, 0])
green = get_image_array(image[:, :, 1])
blue = get_image_array(image[:, :, 2])
output_folder.mkdir(exist_ok=True)
cpp_file_path = output_folder / (file_path.stem.lower() + ".h")
array_name = file_path.stem.upper()
file_contents = CPP_SKELETON.format(
array_name=array_name,
array_red=red,
array_green=green,
array_blue=blue,
height=image.shape[0],
width=image.shape[1],
)
with open(cpp_file_path, "w") as f:
f.write(file_contents)
def main():
parser = argparse.ArgumentParser(description="Generate CPP from image")
parser.add_argument("-i", "--input-folder", type=Path, required=True, help="Path to the input folder of images")
parser.add_argument("-o", "--output-folder", type=Path, required=True, help="Path to the input folder of images")
args = parser.parse_args()
folder: Path = args.input_folder
for filename in os.listdir(folder):
process_file(folder / filename, args.output_folder)
if __name__ == "__main__":
main()