-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_block_colors.py
88 lines (65 loc) · 1.75 KB
/
create_block_colors.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
import json, os, sys
from typing import *
from PIL import Image
colors = {}
#palettes = {}
mcl2_path = None
try:
mcl2_path = sys.argv[1]
except IndexError:
print("Provide the MineClone2 path to the script as an argument")
exit(1)
blocks = None
with open("./generate_json_blocks/availlable_blocks.json") as base_blocks:
blocks = json.loads(base_blocks.read())
#print(blocks)
def texture_to_color(path: str) -> Optional[Tuple[int, int, int]]:
try:
img = Image.open(path).convert("RGBA")
pixels = img.load()
"""
if "palette" in name:
palette = []
for y in range(0, img.size[1]):
for x in range(0, img.size[0]):
r, g, b, a = pixels[x, y]
palette.append((r, g, b))
palettes[name] = palette
else:
"""
r_total = 0
g_total = 0
b_total = 0
count = 0
for x in range(0, img.size[0]):
for y in range(0, img.size[1]):
r, g, b, a = pixels[x, y]
if a > 0:
r_total += r / 255 * a
g_total += g / 255 * a
b_total += b / 255 * a
count += a / 255
average_color = None
if count > 0:
average_color = (int(r_total / count), int(g_total / count), int(b_total / count))
else:
average_color = (255, 255, 255)
return average_color
img.close()
except IOError:
return None
print("Finding files...")
file_count = 0
for root, directories, files in os.walk(mcl2_path+"/mods"):
if root.endswith("/textures"):
for name in files:
for itemname in blocks:
if blocks[itemname] == name:
colors[itemname] = texture_to_color(os.path.join(root, name))
file_count += 1
with open("block_colors.json", "w") as colorfile:
colorfile.write(json.dumps(colors))
colorfile.close()
print(f"Files: {file_count}")
#with open("palettes.json", "w") as palettefile:
# palettefile.write(json.dumps(palettes))