-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_color_converter.py
111 lines (94 loc) · 3.27 KB
/
run_color_converter.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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"""Run the image color converter from RGB to RAL Classic color space."""
import argparse
import os
import sys
import numpy as np
from scipy.spatial import KDTree
import color_converter as cc
def process_image(
img_path: str,
ral_tree: KDTree,
ral_rgb_values: list[tuple[int, int, int]],
output_folder: str,
suffix: str = "",
) -> None:
"""Process a single image and save the converted image."""
try:
img = cc.load_image(img_path)
img_array = np.array(img)
new_img_array = cc.convert_image_to_ral(img_array, ral_tree, ral_rgb_values)
img_filename, img_ext = os.path.splitext(os.path.basename(img_path))
output_filename = f"{img_filename}{suffix}{img_ext}"
output_path = os.path.join(output_folder, output_filename)
cc.save_image(new_img_array, output_path)
print(f"Processed and saved: {output_path}")
except FileNotFoundError:
print(f"File not found: {img_path}")
except IOError:
print(f"IO error occurred while processing {img_path}")
except ValueError as e:
print(f"Value error: {e} while processing {img_path}")
def main(args: argparse.Namespace) -> int:
"""Run the image color converter."""
input_folder = args.input
output_folder = args.output
image_file = args.file
suffix = args.suffix
# Create paths (and folders)
dir_ = os.path.dirname(os.path.abspath(__file__))
colors_path = os.path.join(dir_, "ral_classic.csv")
os.makedirs(output_folder, exist_ok=True)
# Load RAL colors
ral_colors = cc.load_ral_colors(colors_path)
ral_rgb_values = list(ral_colors.values())
ral_tree = KDTree(ral_rgb_values)
# Get images to process
if image_file:
img_paths = [os.path.join(input_folder, image_file)]
else:
img_paths = [
os.path.join(input_folder, f)
for f in os.listdir(input_folder)
if f.endswith((".jpg", ".jpeg", ".png", ".webp"))
]
# Process images
for idx, img_path in enumerate(img_paths, start=1):
img_filename = os.path.basename(img_path)
print(f"Processing file {idx}/{len(img_paths)}: {img_filename}")
process_image(img_path, ral_tree, ral_rgb_values, output_folder, suffix)
return 0
if __name__ == "__main__":
default_in = os.path.join(os.path.dirname(os.path.abspath(__file__)), "images")
default_out = os.path.join(os.path.dirname(os.path.abspath(__file__)), "converted")
default_file = os.path.join(default_in, "example1.jpg")
parser = argparse.ArgumentParser(
description="Convert images from RGB to RAL Classic color space."
)
parser.add_argument(
"-i",
"--input",
type=str,
default=default_in,
help="Input folder containing images.",
)
parser.add_argument(
"-o",
"--output",
type=str,
default=default_out,
help="Output folder to save converted images.",
)
parser.add_argument(
"-f",
"--file",
type=str,
help="Specific image file to convert.",
)
parser.add_argument(
"-s",
"--suffix",
type=str,
default="_ral",
help="Suffix to append to the filename before the extension.",
)
sys.exit(main(parser.parse_args()))