summaryrefslogtreecommitdiffstats
path: root/resource/extract.py
blob: e867d10d42c993b5f2ac5b41c243cb91845c3dfd (plain)
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
#!/usr/bin/env python3

import json
import os
import sys

from PIL import Image


if len(sys.argv) != 4:
	sys.exit('Usage: extract.py <blocks.json> <asset directory> <colors.json>')

def mean_color(texture):
	path = os.path.join(sys.argv[2], texture + '.png')
	im = Image.open(path)

	data = im.convert('RGBA').getdata()
	a = sum([a for (r, g, b, a) in data])
	if a == 0:
		return None

	r = sum([r * a for (r, g, b, a) in data])
	g = sum([g * a for (r, g, b, a) in data])
	b = sum([b * a for (r, g, b, a) in data])
	return {
		'r': r / a,
		'g': g / a,
		'b': b / a,
	}


with open(sys.argv[1]) as f:
	blocks = json.load(f)

output = {}

for name, info in blocks.items():
	id = 'minecraft:' + name

	output[id] = {
		'color': {'r': 0, 'g': 0, 'b': 0},
		'opaque': False,
		'green': False,
		'blue': False,
	}

	if info is None:
		continue

	color = mean_color(info.get('texture', name))
	if color:
		output[id]['color'] = color
		output[id]['opaque'] = True
		output[id]['green'] = info.get('green', False)
		output[id]['blue'] = info.get('blue', False)

with open(sys.argv[3], 'w') as f:
	json.dump(output, f)