-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstandard_text.py
More file actions
51 lines (39 loc) · 1.48 KB
/
Copy pathstandard_text.py
File metadata and controls
51 lines (39 loc) · 1.48 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
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont
class Std_Text(object):
def __init__(self, font_path):
self.height = 64
self.max_width = 960
self.border_width = 5
self.char_list = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
font_height = self.get_valid_height(font_path)
self.font = ImageFont.truetype(font_path, font_height)
def get_valid_height(self, font_path):
font = ImageFont.truetype(font_path, self.height - 4)
_, font_height = font.getsize(self.char_list)
if font_height <= self.height - 4:
return self.height - 4
else:
return int((self.height - 4)**2 / font_height)
def draw_text(self, text):
assert len(text) != 0
char_x = self.border_width
bg = Image.new("RGB", (self.max_width, self.height), color=(127, 127, 127))
draw = ImageDraw.Draw(bg)
for char in text:
draw.text((char_x, 2), char, fill=(0, 0, 0), font=self.font)
char_size = self.font.getsize(char)[0]
char_x += char_size
canvas = np.array(bg).astype(np.uint8)
char_x += self.border_width
canvas = canvas[:, :char_x, :]
return canvas
def main():
font_path = 'arial.ttf'
std_text = Std_Text(font_path)
tmp = std_text.draw_text('qwertyuiopasdfghjklzxcvbnm')
print(tmp.shape)
cv2.imwrite('tmp.jpg', tmp)
if __name__ == '__main__':
main()