-
Notifications
You must be signed in to change notification settings - Fork 2
/
pillowtext.py
208 lines (175 loc) · 4.49 KB
/
pillowtext.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
from PIL import Image, ImageFont, ImageDraw
import os
import re
import random
text_size = 16
font = ImageFont.truetype('fonts/Minecraft.otf', text_size)
color_codes = {
'0': (0, 0, 0),
'1': (0, 0, 170),
'2': (0, 170, 0),
'3': (0, 170, 170),
'4': (170, 0, 0), # red
'5': (170, 0, 170),
'6': (255, 170, 0), # gold
'7': (170, 170, 170),
'8': (85, 85, 85),
'9': (85, 85, 254),
'a': (85, 254, 85),
'b': (85, 254, 254),
'c': (254, 85, 85),
'd': (254, 85, 254),
'e': (255, 255, 85),
'f': (255, 255, 255)
}
def add_newlines(formatted_text, get_text_width_func):
new_text = ''
is_bold = False
current_x = 0
next_character_is_formatter = False
for character in formatted_text:
if character == '&':
next_character_is_formatter = True
elif next_character_is_formatter:
if character == 'l':
is_bold = True
elif character == 'r':
is_bold = False
next_character_is_formatter = False
elif character == '\n':
current_x = 0
else:
character_width = get_text_width_func(character)
if is_bold:
character_width += 2
current_x += character_width
if current_x > 1000:
new_text += '\n'
current_x = 0
new_text += character
return new_text
def get_formatted_width(formatted_text, get_text_width_func):
width = 0
is_bold = False
current_x = 0
next_character_is_formatter = False
for character in formatted_text:
if character == '&':
next_character_is_formatter = True
elif next_character_is_formatter:
if character == 'l':
is_bold = True
elif character == 'r':
is_bold = False
next_character_is_formatter = False
elif character == '\n':
current_x = 0
else:
character_width = get_text_width_func(character)
if is_bold:
character_width += 2
current_x += character_width
if current_x > width:
width = current_x
return width
def create_image_from_formatted_text(formatted_text, transparent=False):
im = Image.new(
'RGBA',
(1, 1),
(0,0,0,0)
)
draw = ImageDraw.Draw(im)
def get_text_width(string):
return draw.textsize(string, font)[0]
formatted_text = add_newlines(formatted_text, get_text_width)
unformatted_text = re.sub(r'&.', '', formatted_text)
width, height = draw.textsize(unformatted_text, font)
width = get_formatted_width(formatted_text, get_text_width)
im = im.resize((width + (text_size // 8) + 2, height + (text_size // 8)))
draw = ImageDraw.Draw(im)
current_x = 0
current_y = 0
next_character_is_formatter = False
current_color = (255,255,255)
is_bold = False
for character in formatted_text:
if character == '&':
next_character_is_formatter = True
elif next_character_is_formatter:
if character in color_codes:
current_color = color_codes[character]
elif character == 'r':
current_color = (255,255,255)
is_bold = False
elif character == 'l':
is_bold = True
next_character_is_formatter = False
elif character == '\n':
current_x = 0
current_y += text_size + (text_size // 4)
else:
character_offset_y = 0
if character in '+-':
# the font is weird /shrug
character_offset_y = -2
dark_color = (
int(current_color[0] * .25),
int(current_color[1] * .25),
int(current_color[2] * .25)
)
shadow_x = current_x + (text_size // 8)
shadow_y = current_y + (text_size // 8) + character_offset_y
draw.text(
(shadow_x, shadow_y),
character,
font=font,
fill=dark_color,
)
if is_bold:
draw.text(
(shadow_x + 2, shadow_y),
character,
font=font,
fill=dark_color,
)
draw.text(
(current_x, current_y + character_offset_y),
character,
font=font,
fill=current_color
)
if is_bold:
draw.text(
(current_x + 2, current_y + character_offset_y),
character,
font=font,
fill=current_color
)
character_width = get_text_width(character)
if is_bold:
character_width += 2
current_x += character_width
return im
def add_background(foreground, filename=None):
if not filename:
filename = random.choice(os.listdir('backgrounds'))
background = Image.open('backgrounds/' + filename)
background = background.convert('RGBA')
new_foreground = Image.new('RGBA', (foreground.width, background.height), (0,0,0,0))
text_x = 4
text_y = (background.height - foreground.height)
new_foreground.paste(
foreground,
(
text_x,
text_y
)
)
background.alpha_composite(new_foreground)
output = background.crop((
0,
background.height - foreground.height,
foreground.width + 4,
background.height # (background.height - foreground.height)
))
return output