-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
83 lines (66 loc) · 2.35 KB
/
utils.py
File metadata and controls
83 lines (66 loc) · 2.35 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
import numpy as np
import cv2
from kivy.utils import platform
def rotate_image(input_path, output_path):
"""
Rotates an image 90 degrees clockwise and saves it.
Swaps dimensions (e.g., 2000x1000 becomes 1000x2000).
Args:
input_path (str): Path to the input image file.
output_path (str): Path where the rotated image will be saved.
"""
try:
# load
img = cv2.imread(input_path)
# rotate
rotated_img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
# save
cv2.imwrite(output_path, rotated_img)
except Exception as e:
print(f"Error: {e}")
def get_sys_theme():
"""
Gets the theme of the device.
Returns:
"Dark": Dark mode
"Light": Light mode
"""
if platform == "android":
from jnius import autoclass # type: ignore
Configuration = autoclass("android.content.res.Configuration")
activity = autoclass("org.kivy.android.PythonActivity").mActivity
night_mode_flags = (
activity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK
)
if night_mode_flags == Configuration.UI_MODE_NIGHT_YES:
return "Dark"
else:
return "Light"
elif platform == "win":
import winreg
try:
registry = winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER)
key = winreg.OpenKey(
registry,
r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize",
)
value, _ = winreg.QueryValueEx(key, "AppsUseLightTheme")
return "Light" if value == 1 else "Dark"
except Exception as e:
print("Error getting theme:", e)
return "Light" # Default fallback
else:
print("Getting the theme is only working on Android and Windows")
return "Dark"
def downscale_cv2(input_path: str, scale: float):
"""
Fast downscaling using OpenCV
"""
img = cv2.imread(input_path)
size_old = (img.shape[1], img.shape[0])
new_size = (int(img.shape[1] * scale), int(img.shape[0] * scale))
img_resized = cv2.resize(img, new_size, interpolation=cv2.INTER_AREA)
img_rgb = cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB)
flipped = np.flip(img_rgb, 0)
buf = flipped.tobytes()
return buf, size_old, new_size