-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_create_prop_reference_sheet.py
More file actions
97 lines (76 loc) · 3.08 KB
/
_create_prop_reference_sheet.py
File metadata and controls
97 lines (76 loc) · 3.08 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import math
from PIL import Image, ImageFont, ImageDraw
# Some settings
sprite_dir = 'sprites'
out_dir = 'files/prop_reference'
thumb_size = 72
font_size = 12
quality = 90
padding = 30
section_size = thumb_size + padding * 2
section_mid = section_size / 2
# Get sprites by group
sprites = os.listdir(sprite_dir)
sprite_groups = dict()
for sprite_file in sprites:
sprite_array = sprite_file[:-4].split('_', 4)
group_name = sprite_array[0]
prop_set = int(sprite_array[1])
prop_group = int(sprite_array[2])
prop_index = int(sprite_array[3])
prop_name = sprite_array[4]
if group_name not in sprite_groups:
group = sprite_groups[group_name] = []
else:
group = sprite_groups[group_name]
# group.append(prop_name)
group.append((prop_set, prop_group, prop_index, prop_name, sprite_file))
# print(group_name, prop_set, group_index, prop_index, prop_name)
# Render
for group_name, sprites in sprite_groups.items():
sorted_sprites = sorted(sprites, key=lambda item: (int(item[3].partition(' ')[0]) if item[3][0].isdigit() else float('inf'), item))
count = len(sorted_sprites)
num_cols = math.ceil(math.sqrt(count))
num_rows = math.ceil(count / num_cols)
col_size = section_size
sheet_image = Image.new('RGBA', (1, 1), (0, 0, 0))
font = ImageFont.truetype('files/DejaVuSans.ttf', font_size)
draw = ImageDraw.Draw(sheet_image)
for prop_set, prop_group, prop_index, name, file in sorted_sprites:
w, h = draw.textsize(name, font=font)
if w > col_size:
col_size = w
size = (col_size * num_cols, section_size * num_rows)
sheet_image = Image.new('RGBA', size, (0, 0, 0))
composite_image = Image.new('RGBA', size, (0, 0, 0, 0))
composite_draw = ImageDraw.Draw(composite_image)
pad_x = int((col_size - thumb_size) / 2)
pad_y = padding
col = 0
row = 0
for prop_set, prop_group, prop_index, name, file in sorted_sprites:
x = col * col_size + pad_x
y = row * section_size + pad_y
thumb = Image.open(sprite_dir + '/' + file)
thumb.thumbnail((thumb_size, thumb_size))
if thumb.size[0] != thumb_size or thumb.size[1] != thumb_size:
new_image = Image.new('RGBA', (thumb_size, thumb_size), 0x000000)
new_image.paste(thumb, (int((thumb_size - thumb.size[0]) / 2), int((thumb_size - thumb.size[1]) / 2)))
thumb = new_image
composite_image.paste(thumb, (x, y))
# sheet_image.paste(thumb, (x, y))
sheet_image = Image.alpha_composite(sheet_image, composite_image)
draw = ImageDraw.Draw(sheet_image)
w, h = draw.textsize(name, font=font)
draw.text((x + (thumb_size - w) / 2, y + thumb_size - h + padding / 2), name, (255, 255, 255), font=font)
indices_text = '{} {} {}'.format(prop_set, prop_group, prop_index)
w, h = draw.textsize(indices_text, font=font)
draw.text((x + (thumb_size - w) / 2, y + thumb_size + padding / 2), indices_text, (255, 255, 255), font=font)
composite_draw.rectangle(((0, 0), size), (0, 0, 0, 0))
col += 1
if col == num_cols:
col = 0
row += 1
# sheet_image.save('%s/%s.png' % (out_dir, group_name), optimize=True, compress_level=9)
sheet_image.save('%s/%s.jpg' % (out_dir, group_name), quality=quality, optimize=True, progressive=True)