-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_export_alpha_masks.py
More file actions
78 lines (60 loc) · 2.53 KB
/
debug_export_alpha_masks.py
File metadata and controls
78 lines (60 loc) · 2.53 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
import configparser
import os
def debug_export_alpha_masks(
source_folder: str, destination_folder: str, fuzzy: bool = False
):
if not os.path.exists(source_folder):
raise FileNotFoundError(f"Source folder '{source_folder}' does not exist.")
if not os.path.exists(destination_folder):
os.makedirs(destination_folder, exist_ok=True)
print(f"Destination folder '{destination_folder}' created.")
# iterate over all files in source folder and extract alpha masks
for root, dirs, files in os.walk(source_folder):
for file_name in files:
file_path = os.path.join(root, file_name)
# load the image
try:
from PIL import Image
img = Image.open(file_path).convert("RGBA")
except Exception as e:
print(f"Failed to load image {file_name}: {e}")
continue
# create a mask from the alpha channel
try:
mask = img.split()[-1] # Get the alpha channel
if fuzzy:
mask = mask.point(lambda p: 255 if p > 0 else 0)
except Exception as e:
print(f"Failed to create mask for {file_name}: {e}")
continue
# save the mask
try:
# use same path structure as the source folder
dest = os.path.join(
destination_folder, os.path.relpath(file_path, source_folder)
)
dest, ext = os.path.splitext(dest)
dest = dest + ".png"
os.makedirs(os.path.dirname(dest), exist_ok=True)
mask.save(dest)
print(f"Generated mask for {file_name} at {dest}")
except Exception as e:
print(f"Failed to save mask for {file_name}: {e}")
def main():
print("\n[DEBUG EXPORT ALPHA MASKS] Generating masks...")
config = configparser.ConfigParser()
config.read("config.ini")
source_folder = config.get(
"Stronghold Definitive Edition",
"debug_unpacked_folder",
fallback="dev/unpacked/Stronghold Definitive Edition",
)
destination_folder = config.get(
"Stronghold Definitive Edition",
"debug_exported_alpha_masks_folder",
fallback="dev/debug_exported_alpha_masks_folder/Stronghold Definitive Edition",
)
debug_export_alpha_masks(source_folder, destination_folder)
print("\n[DEBUG EXPORT ALPHA MASKS] Finished generating masks.")
if __name__ == "__main__":
main()