-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdrawtext.py
More file actions
executable file
·50 lines (38 loc) · 1.74 KB
/
drawtext.py
File metadata and controls
executable file
·50 lines (38 loc) · 1.74 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
# -*- coding: utf-8 -*-
import os
from PIL import Image, ImageDraw, ImageFont
#CANVAS_SIZE = 1024
CANVAS_SIZE = 1000
CHAR_SIZE = CANVAS_SIZE
if __name__ == '__main__':
#text = "桌"
text = chr(int('f92d',16))
filename = ("000000" + str(hex(ord(text)))[2:])[-6:]
filename = "U_" + filename + ".bmp"
# create an image
out = Image.new("L", (CANVAS_SIZE, CANVAS_SIZE), 255)
# get a font
#src_font = "/Users/chaopan/PycharmProjects/zi2zi/data/raw_fonts/造字工房尚黑纤细超长体.ttf"
#src_font = "/Users/chunyuyao/Documents/maxai/pcgreat/zi2zi/data/max_fonts/jason2.ttf"
#src_font = "/Users/chunyuyao/Documents/maxai/pcgreat/zi2zi/data/max_fonts/noto.otf"
src_font = "/Users/chunyuyao/Documents/maxai/pcgreat/zi2zi/data/max_fonts/SweiGothicLegCJKjp-Regular.ttf"
#print(os.path.isfile(src_font))
font = ImageFont.truetype(src_font, CHAR_SIZE)
ascent, descent = font.getmetrics()
(width, baseline), (offset_x, offset_y) = font.font.getsize(text)
left, top, right, bottom = font.getmask(text).getbbox()
#print("ascent, descent:", ascent, descent)
#print("width, baseline, offset_x, offset_y:", width, baseline, offset_x, offset_y)
#print("left, top, right, bottom:", left, top, right, bottom)
# get a drawing context
d = ImageDraw.Draw(out)
#d.text((left, top), text, font=font, fill=(0))
#d.text((0, 0), text, font=font, fill=(0))
#d.text((0, -1 * offset_y), text, font=font, fill=(0))
total_height = ascent + descent
new_offset_y = -1 * ((total_height-CHAR_SIZE)/2)
#new_offset_y = -1 * (276-51)/2
#new_offset_y = -1 * (100-50)/2
#print("new_offset_y:", new_offset_y)
d.text((0, new_offset_y), text, font=font, fill=(0))
out.save(filename)