This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
62 lines (56 loc) · 1.97 KB
/
utils.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
from PilLite import Image
import nbtlib
from schema import schem_schematic, schem_te, schem_schematic_file
def rgb32to16(rgb):
r = int(((rgb & 0x00FF0000) >> 16) / 255 * 15)
g = int(((rgb & 0x0000FF00) >> 8) / 255 * 15)
b = int(((rgb & 0x000000FF) >> 0) / 255 * 15)
return (r << 8) + (g << 4) + b
def rgb32to16ex(rgb):
r = int(((rgb & 0xFF000000) >> 24) / 255 * 15)
g = int(((rgb & 0x00FF0000) >> 16) / 255 * 15)
b = int(((rgb & 0x0000FF00) >> 8) / 255 * 15)
return (r << 8) + (g << 4) + b
def generate_nbt(img: Image, transparent=False):
(width, length) = img.size
print(f"Image dimension:{width}*{length}")
blocks = bytearray()
data = bytearray()
addblocks_raw = bytearray()
te = list()
for l in range(length):
print('.', end='')
for w in range(width):
pixel = img.get_pixel((w, l))
color_code = rgb32to16ex(pixel) if transparent else rgb32to16(pixel)
if not transparent or pixel & 0xff == 255:
blocks.append(0x7F)
data.append(0x0B)
addblocks_raw.append(3)
te.append(schem_te({
'id': 'terraqueous:type.colored',
'rgb16': color_code,
'x': w,
'y': 0,
'z': l
}))
else:
blocks.append(0)
data.append(0)
addblocks_raw.append(0)
print('done!')
addblocks = bytearray()
for i in range(0, len(addblocks_raw), 2):
addblocks.append(addblocks_raw[i] + (addblocks_raw[i + 1] << 4) if len(addblocks_raw) - i >= 2 else 3)
te_list = nbtlib.List(te)
nbt = schem_schematic({
'Height': 1,
'Length': length,
'Width': width,
'Materials': 'Alpha',
'TileEntities': te_list,
'AddBlocks': addblocks,
'Blocks': blocks,
'Data': data
})
return schem_schematic_file({'Schematic': nbt})