-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild_command.py
More file actions
235 lines (157 loc) · 7.43 KB
/
build_command.py
File metadata and controls
235 lines (157 loc) · 7.43 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# This functions create commands, that can be executed in a terminal or a subprocess.
# There are two reasons for this approach for compression:
# 1. CLI tools offer more parameters for adjusting the compression
# 2. Different compression algorithms require specific libraries, which sometimes causes conflicts.
# This approach offers the possibility to use different environments (for example: conda)
#
# The main disadvantage are the specific paths that must be provided.
import paths
def build_command_jpeg(image_path, output_path, **kwargs):
# cjpeg does not take .png as input -> convert to .ppm first
command = f"""convert {image_path} {output_path[:-3]}ppm && cjpeg """
command += f"""-q {kwargs['quality']} -outfile {output_path} """
if "smooth" in kwargs:
command += f"""-smooth {kwargs["smooth"]} """
if "grayscale" in kwargs and kwargs["grayscale"] == True:
command += f"""-grayscale """
if "rgb" in kwargs and kwargs["rgb"] == True:
command += f"""-rgb """
if "progressive" in kwargs and kwargs["progressive"] == True:
command += f"""-progressive """
if "optimize" in kwargs and kwargs["optimize"] == True:
command += f"""-optimize """
if "arithmetic" in kwargs and kwargs["arithmetic"] == True:
command += f"""-arithmetic """
command += f"""{output_path[:-3]}ppm """
return command
def build_command_j2k(input_path, output_path_j2k, **kwargs):
command = "opj_compress "
command += f"""-i {input_path} -o {output_path_j2k} """
if "ratio" in kwargs:
command += f"""-r {kwargs["ratio"]} """
if "psnr" in kwargs:
command += f"""-q {kwargs["psnr"]} """
if "number_of_resolutions" in kwargs:
command += f"""-n {kwargs["number_of_resolutions"]} """
if "block_width" in kwargs and "block_height" in kwargs:
command += f"""-b {kwargs["block_width"]},{kwargs["block_height"]} """
if "tile_width" in kwargs and "tile_height" in kwargs:
command += f"""-t {kwargs["tile_width"]},{kwargs["tile_height"]} """
if "subsampling_x" in kwargs and "subsampling_y" in kwargs:
command += f"""-s {kwargs["subsampling_x"]},{kwargs["subsampling_y"]} """
if "progression_order" in kwargs:
command += f"""-p {kwargs["progression_order"]} """
return command
def build_command_heif(input_path, output_path_heif, **kwargs):
command = "heif-enc "
if "quality" in kwargs:
command += f"""-q {kwargs["quality"]} """
if "no_alpha" in kwargs and kwargs["no_alpha"] == True:
command += f"""--no-alpha """
if "bit_depth" in kwargs:
command += f"""-b {kwargs["bit_depth"]} """
if "chroma_downsampling" in kwargs:
command += f"""-C {kwargs["chroma_downsampling"]} """
if "cut_tiles" in kwargs:
command += f"""--cut-tiles {kwargs["cut_tiles"]} """
command += f"""-o {output_path_heif} {input_path} """
return command
def build_command_webp(input_path, output_path_webp, **kwargs):
command = "cwebp "
if "preset" in kwargs:
command += f"""-preset {kwargs["preset"]} """
command += f"""-size 1024 """
if "method" in kwargs:
command += f"""-m {kwargs["method"]} """
if "segments" in kwargs:
command += f"""-segments {kwargs["segments"]} """
if "psnr" in kwargs:
command += f"""-psnr {kwargs["psnr"]} """
if "filter_strength" in kwargs:
command += f"""-f {kwargs["filter_strength"]} """
if "sharpness" in kwargs:
command += f"""-sharpness {kwargs["sharpness"]} """
if "analysis_pass" in kwargs:
command += f"""-pass {kwargs["analysis_pass"]} """
if "no_alpha" in kwargs and kwargs["no_alpha"] == True:
command += f"""-noalpha """
if "sns" in kwargs:
command += f"""-sns {kwargs["sns"]} """
command += f""""{input_path}" -o "{output_path_webp}" """
return command
def build_command_jxl(input_path, output_path_jxl, **kwargs):
command = set_conda_env_base()
command += f"""cjxl "{input_path}" "{output_path_jxl}" """
if "quality" in kwargs:
command += f"""-q {kwargs["quality"]} """
if "distance" in kwargs:
command += f"""-d {kwargs["distance"]} """
if "effort" in kwargs:
command += f"""-e {kwargs["effort"]} """
if "alpha_distance" in kwargs:
command += f"""-a {kwargs["alpha_distance"]} """
if "progressive" in kwargs and kwargs["progressive"] == True:
command += f"""-p """
if "compress_boxes" in kwargs:
command += f"""--compress_boxes {kwargs["compress_boxes"]} """
if "brotli_effort" in kwargs:
command += f"""--brotli_effort {kwargs["brotli_effort"]} """
if "override_bit_depth" in kwargs:
command += f"""--override_bitdepth {kwargs["override_bit_depth"]} """
if "resampling" in kwargs:
command += f"""--resampling {kwargs["resampling"]} """
return command
def build_command_avif(input_path, output_path_avif, **kwargs):
command = f"""avifenc """
if "quality_color" in kwargs:
command += f"""--qcolor {kwargs["quality_color"]} """
if "quality_alpha" in kwargs:
command += f"""--qalpha {kwargs["quality_alpha"]} """
if "yuv_format" in kwargs:
command += f"""--yuv {kwargs["yuv_format"]} """
if "depth" in kwargs:
command += f"""--depth {kwargs["depth"]} """
if "premultiply" in kwargs and kwargs["premultiply"] == True:
command += f"""--premultiply """
if "tile_cols" in kwargs:
command += f"""--tilecolslog2 {kwargs["tile_cols"]} """
if "tile_rows" in kwargs:
command += f"""--tilerowslog2 {kwargs["tile_rows"]} """
if "speed" in kwargs:
command += f"""--speed {kwargs["speed"]} """
if "range" in kwargs:
command += f"""--range {kwargs["range"]} """
if "sharp_yuv" in kwargs and kwargs["sharp_yuv"] == True:
command += f"""--sharpyuv """
if "progressive" in kwargs and kwargs["progressive"] == True:
command += f"""--progressive """
if "target_size" in kwargs:
command += f"""--target-size {kwargs["target_size"]} """
command += f""" "{input_path}" "{output_path_avif}" """
return command
def build_command_jai(input_path, output_path_jai, **kwargs):
command = set_conda_env_jpeg_ai_vm()
command += f"""cd {paths.jpeg_ai_reference_software} && """
command += f"""python -m src.reco.coders.encoder {input_path} {output_path_jai} """
if "target_bpp" in kwargs:
command += f"""--set_target_bpp {kwargs["target_bpp"]} """
return command
def build_command_jxl_decompress(input_path, output_path_png):
command = set_conda_env_base()
command += f"""djxl "{input_path}" "{output_path_png}" """
return command
def build_command_heif_decompress(input_path, output_path_png):
command = f"""heif-dec "{input_path}" -o "{output_path_png}" """
return command
def build_command_j2k_decompress(input_path, output_path_png):
command = f"""opj_decompress -i "{input_path}" -o "{output_path_png}" """
return command
def build_command_jai_decompress(input_path, output_path_png):
command = set_conda_env_jpeg_ai_vm()
command += f"""cd {paths.jpeg_ai_reference_software} && """
command += f"""python -m src.reco.coders.decoder {input_path} {output_path_png}"""
return command
def set_conda_env_base():
return f"""source {paths.conda_envs} base && """
def set_conda_env_jpeg_ai_vm():
return f"""source {paths.conda_envs} jpeg_ai_vm && """