-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrockbox_theme_rescaler.py
More file actions
202 lines (173 loc) · 7.81 KB
/
Copy pathrockbox_theme_rescaler.py
File metadata and controls
202 lines (173 loc) · 7.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Rockbox Theme Rescaler
---------------------
Author: thinkVHS (updated)
Date: 2025-09-03
Description:
Rescales BMP images and coordinates in .wps, .sbs, .fms files
of a Rockbox theme. Copies other files without modifying them.
"""
import argparse
from pathlib import Path
from PIL import Image
from tqdm import tqdm
import shutil
import re
import subprocess, os
def get_bit_depth(file_path):
result = subprocess.run(
['identify', '-format', '%z', file_path],
capture_output=True, text=True
)
if result.returncode == 0:
bit_depth = int(result.stdout.strip())
return bit_depth
else:
return None
def get_width(file_path):
result = subprocess.run(
['identify', '-format', '%w', file_path],
capture_output=True, text=True
)
if result.returncode == 0:
width = int(result.stdout.strip())
return width
else:
return None
def resize_bmp(input_path, output_path, factor, filter_bg, filter_icon):
"""
Rescale a BMP image. If detected as an icon (≤32px), NEAREST is used;
otherwise, the configured filter is applied.
"""
factor = (1.5,1.5)
width = get_width(input_path)
bit_depth = get_bit_depth(input_path)
magick_filter = "Point"
os.system(f"mkdir -p \"{'/'.join(str(output_path).split('/')[:-1])}\"")
if bit_depth != 1:
if width <= 32:
os.system(f"magick '{input_path}' -filter Point -resize {int(factor[0]*100)}% '{output_path}'")
else:
os.system(f"magick '{input_path}' -filter {filter_bg} -resize {int(factor[0]*100)}% '{output_path}'")
else:
if width <= 32:
os.system(f"magick '{input_path}' -filter Point -resize {int(factor[0]*100)}% '{output_path}'")
else:
os.system(f"magick '{input_path}' -filter {filter_bg} -resize {int(factor[0]*100)}% '{output_path}'")
os.system(f"magick '{output_path}' -monochrome -colors 2 -depth 1 '/tmp/{str(output_path).split('/')[-1]}'")
os.system(f"mv '/tmp/{str(output_path).split('/')[-1]}' '{output_path}'")
def scale_value(val, factor):
val = val.strip()
if val == '-' or val.endswith('%'):
return val # do not rescale special values
try:
return str(int(int(val) * factor))
except ValueError:
return val
def get_image_height(image_path):
"""Retrieve the height of the image at the given path."""
try:
with Image.open(image_path) as img:
return img.height
except Exception as e:
print(f"Error opening image {image_path}: {e}")
return None # Return None if there's an error
def rescale_wps_file(file_path, out_path, factor_x, factor_y, FILTER_BG, FILTER_ICON):
"""
Rescale coordinates inside .wps, .sbs, or .fms files.
"""
try:
text = file_path.read_text(encoding="utf-8")
except UnicodeDecodeError:
text = file_path.read_text(encoding="latin-1")
processed_bmps = []
patterns = {
"%V": ["x","y","width","height","fontid"],
"%Vl": ["id","x","y","width","height","fontid"],
"%Vi": ["label","x","y","width","height","fontid"],
"%dr": ["x","y","width","height","colour1","colour2"],
"%pb": ["x","y","width","height","filename"],
"%pv": ["x","y","width","height","filename"],
"%x": ["label","filename","x","y"],
"%xl": ["label","filename","x","y","nimages"],
"%Cl": ["xpos","ypos","maxwidth","maxheight","halign","valign"],
"%T": ["label","x","y","width","height","action","options"],
"%Lb": ["viewport","width","height","tile"],
"%XX": ["x","y","width","height","filename","options"]
}
for key, params in patterns.items():
regex = re.compile(rf"{key}\((.*?)\)")
def repl(match):
parts = [p.strip() for p in match.group(1).split(",")]
for i, name in enumerate(params):
if i < len(parts):
if name in ["x","y","width","height","xpos","ypos","maxwidth","maxheight"] and not ((key == "%xl") and (len(parts) == 5)):
factor = factor_x if "x" in name else factor_y
parts[i] = scale_value(parts[i], factor)
elif name == "nimages":
# Handle the nimages scaling
image_filename = parts[1] # Assuming the filename is the second parameter
image_path = file_path.parent / file_path.stem / image_filename # Construct the full path
original_height = get_image_height(image_path) # Get the original height
scaled_height = int(original_height * factor_y)
# Find the next best integer multiple of nimages
nimages = int(parts[i]) if parts[i].isdigit() else 1
if scaled_height % nimages != 0:
factor_new = ((scaled_height // nimages) * nimages ) / original_height
height_new = ((scaled_height // nimages) * nimages )
else:
factor_new = factor_x
resize_bmp(image_path, Path(out_path.parent) / Path(out_path.stem) / image_filename, (factor_new, factor_new), FILTER_BG, FILTER_ICON)
processed_bmps.append(image_path)
return f"{key}({','.join(parts)})"
text = regex.sub(repl, text)
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(text, encoding="utf-8")
return processed_bmps
def main():
parser = argparse.ArgumentParser(description="Rescale BMP images and WPS coordinates of a Rockbox theme.")
parser.add_argument("input_dir", help="Input theme folder (e.g., MyTheme_240p)")
parser.add_argument("input_res", choices=["240p", "360p"], help="Input resolution")
parser.add_argument("output_res", choices=["240p", "360p"], help="Output resolution")
parser.add_argument("--filter", choices=["NEAREST", "LANCZOS"], default="LANCZOS",
help="Filter for large images (default: LANCZOS)")
args = parser.parse_args()
input_dir = Path(args.input_dir)
if not input_dir.exists():
raise FileNotFoundError(f"The folder {input_dir} does not exist")
output_dir = input_dir.parent / f"{input_dir.name}_{args.output_res}"
output_dir.mkdir(exist_ok=True)
res_map = {"240p": (320, 240), "360p": (480, 360)}
in_w, in_h = res_map[args.input_res]
out_w, out_h = res_map[args.output_res]
factor_x = out_w / in_w
factor_y = out_h / in_h
FILTER_BG = "Point" if args.filter.upper() == "NEAREST" else "Lanczos"
FILTER_ICON = "Point"
files = list(input_dir.rglob("*"))
with tqdm(total=len(files), desc="Processing files") as pbar:
already_processed = []
# First pass: Process .wps, .sbs, .fms files
for file in files:
if not file.is_file():
pbar.update(1)
continue
rel_path = file.relative_to(input_dir)
out_file = output_dir / rel_path
rel_path = file.relative_to(input_dir)
out_file = output_dir / rel_path
if file.suffix.lower() == ".bmp":
if file not in already_processed:
resize_bmp(file, out_file, (factor_x, factor_y), FILTER_BG, FILTER_ICON)
elif file.suffix.lower() in [".wps", ".sbs", ".fms"]:
processed_bmps = rescale_wps_file(file, out_file, factor_x, factor_y, FILTER_BG, FILTER_ICON)
for b in processed_bmps:
already_processed.append(b)
else:
out_file.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(file, out_file)
pbar.update(1)
if __name__ == "__main__":
main()