-
Notifications
You must be signed in to change notification settings - Fork 0
/
fontmoji.py
82 lines (67 loc) · 2.4 KB
/
fontmoji.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
import json
from PIL import Image, ImageFont, ImageDraw
# some emoji codes
herb = '\U0001F33F'
pretzel = '\U0001F968'
peach = '\U0001f351'
FONT_FILE = './NotoColorEmoji.ttf'
FONT_SIZE = 109
FONT = ImageFont.truetype(FONT_FILE, size=FONT_SIZE, layout_engine=ImageFont.LAYOUT_RAQM)
EMOJI = peach
# each emoji letter contains 7 emoji lines,
#the first and the last lines are used for superscripts and subscripts
LETTER_HEIGHT = 7
EMOJI_DISTANCE = 27
LINE_DISTANCE = 20
LINE_HEIGHT = FONT_SIZE * LETTER_HEIGHT + LINE_DISTANCE * (LETTER_HEIGHT - 1)
BACKGROUND_COLOR = (256, 256, 256, 256) #white
#BACKGROUND_COLOR = (0, 0, 0, 256)
LETTER_DISTANCE_WIDTH = EMOJI_DISTANCE * 3
LETTER_DISTANCE = Image.new('RGBA', (LETTER_DISTANCE_WIDTH, LINE_HEIGHT), BACKGROUND_COLOR)
def emojize_letter(cfg):
desc = cfg['desc']
assert(len(desc) == LETTER_HEIGHT)
columns = cfg['columns']
width = (FONT_SIZE + EMOJI_DISTANCE) * columns
img = Image.new('RGBA', (width, LINE_HEIGHT), BACKGROUND_COLOR)
draw = ImageDraw.Draw(img)
for i in range(LETTER_HEIGHT):
line = ''
row = desc[i]
assert(len(row) == columns)
for j in row:
if j == 'x':
line += EMOJI
else:
line += ' '
LINE_SHIFT = (FONT_SIZE + LINE_DISTANCE) * i
draw.text((0, LINE_SHIFT), line, fill = BACKGROUND_COLOR, embedded_color=True, font = FONT)
return img
def concat(imgs):
width = sum([im.width for im in imgs]) + LETTER_DISTANCE_WIDTH * (len(imgs) + 1)
dst = Image.new('RGBA', (width, imgs[0].height), BACKGROUND_COLOR)
dst.paste(LETTER_DISTANCE, (0, 0))
w = LETTER_DISTANCE_WIDTH
for i in imgs:
dst.paste(i, (w, 0))
dst.paste(LETTER_DISTANCE,(w + i.width, 0))
w += i.width + LETTER_DISTANCE_WIDTH
return dst
def concat_lines(imgs):
height = sum([im.height for im in imgs])
dst = Image.new('RGBA', (max([i.width for i in imgs]), height), BACKGROUND_COLOR)
h = 0
for i in imgs:
dst.paste(i, (0, h))
h += i.height
return dst
def emojize(sentence):
with open('./letters.json', 'r') as f:
cfg = json.load(f)
lines = [concat([emojize_letter(cfg[l]) for l in phrase]) for phrase in sentence.upper().split('\n')]
img = concat_lines(lines)
return img
if __name__ == '__main__':
sentence = 'fontmoji'
img = emojize(sentence)
img.save("out.png")