-
Notifications
You must be signed in to change notification settings - Fork 0
/
assetcrafter.py
147 lines (121 loc) · 5.14 KB
/
assetcrafter.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
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
import json
import os
import shutil
import sys
from typing import Any, cast
from PIL import Image
class Asset:
def __init__(self, source: Image.Image | str):
self.image: Image.Image
if isinstance(source, str):
self.image = Image.open(f"src/{source}.png")
else:
self.image = source
@property
def format(self) -> str:
return self.image.mode
@property
def size(self) -> tuple[int, int]:
return self.image.size
def save(self, name: str, format_: str) -> None:
self.image.convert(format_).save(f"out/{name}.png")
class AssetMap(Asset):
def __init__(self, source: Image.Image | str, rows: int, cols: int):
super().__init__(source)
self.rows: int = rows
self.cols: int = cols
@property
def tile_size(self) -> tuple[int, int]:
return self.image.width // self.cols, self.image.height // self.rows
def select(self, row: int, col: int) -> Asset:
width, height = self.tile_size
x, y = width * col, height * row
return Asset(self.image.crop((x, y, x + width, y + height)))
def get_tile_size(asset: Asset) -> tuple[int, int]:
if isinstance(asset, AssetMap):
return asset.tile_size
else:
return asset.size
def create_icon(source: Asset, width: int, height: int, scaling: dict[str, Any] | None = None) -> Asset:
if scaling is None or scaling["smooth"]:
resampling_filter = None
else:
resampling_filter = Image.Resampling.NEAREST
content = source.image.crop(source.image.getbbox())
original_width, original_height = content.size
aspect_ratio = min(width / original_width, height / original_height)
new_width, new_height = int(original_width * aspect_ratio), int(original_height * aspect_ratio)
resized_image = content.resize((new_width, new_height), resampling_filter)
icon = Image.new("RGBA", (width, height), (0, 0, 0, 0))
icon.paste(resized_image, ((width - new_width) // 2, (height - new_height) // 2))
return Asset(icon)
def create_map(sources: list[Asset], content: list[list[list[int]]]) -> AssetMap:
format_: str = "RGBA" if "RGBA" in (source.format for source in sources) else "RGB"
tile_width, tile_height = max({get_tile_size(source) for source in sources})
width, height = tile_width * len(content[0]), tile_height * len(content)
image = Image.new(format_, (width, height))
for row, cols in enumerate(content):
for col, tile in enumerate(cols):
source = sources[tile[0]]
if isinstance(source, AssetMap):
source = source.select(tile[1], tile[2])
image.paste(source.image, (col * tile_width, row * tile_height))
return AssetMap(image, height // tile_height, width // tile_width)
def main() -> None:
path: str = sys.argv[1] if len(sys.argv) >= 2 else "."
os.chdir(path)
try:
with open("assets.json", "r") as asset_config_file:
asset_config = json.load(asset_config_file)
except FileNotFoundError:
print('Asset config "assets.json" not found')
return
except IsADirectoryError:
print('Asset config "assets.json" is a directory')
return
if os.path.exists("out"):
if not os.path.isdir("out"):
print('Cannot write output because "out" is not a directory')
return
shutil.rmtree("out")
os.mkdir("out")
assets: dict[str, Asset] = {}
# Sources
for source in asset_config["sources"]:
try:
if "rows" in source and "cols" in source:
assets[source["name"]] = AssetMap(source["path"], source["rows"], source["cols"])
else:
assets[source["name"]] = Asset(source["path"])
except FileNotFoundError:
print(f'Missing source file "{source["path"]}"')
# Artifacts
for artifact in asset_config["artifacts"]:
sources: list[Asset] = [assets[source] for source in artifact["sources"]]
match artifact["type"]:
case "icon":
if len(sources) > 1:
print("Icon only uses the first source")
if "row" in artifact and "col" in artifact:
source = cast(AssetMap, sources[0]).select(artifact["row"], artifact["col"])
else:
source = sources[0]
assets[artifact["name"]] = create_icon(source, **artifact["attributes"])
case "map":
assets[artifact["name"]] = create_map(sources, **artifact["attributes"])
case _ as type_:
print(f'Unknown artifact type "{type_}"')
return
# Output
for output in asset_config["output"]:
if (source := output["source"]) not in assets:
print(f'Source "{source}" undefined')
return
format_: str = "RGBA" if output.get("alpha", False) else "RGB"
if "row" in output and "col" in output:
asset = cast(AssetMap, assets[output["source"]]).select(output["row"], output["col"])
else:
asset = assets[output["source"]]
asset.save(output["name"], format_)
if __name__ == "__main__":
main()