gyrojeff commited on
Commit
c929237
1 Parent(s): 665559f

feat: add generation script for font sample images

Browse files
Files changed (1) hide show
  1. generate_font_sample_image.py +42 -0
generate_font_sample_image.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from PIL import Image, ImageDraw, ImageFont
3
+ from typing import List
4
+ from font_dataset.font import load_fonts, DSFont
5
+ from tqdm import tqdm
6
+
7
+ os.makedirs("./demo_fonts", exist_ok=True)
8
+
9
+ font_list, exclusion_rule = load_fonts()
10
+ font_list: List[DSFont] = list(filter(lambda x: not exclusion_rule(x), font_list))
11
+ font_list.sort(key=lambda x: x.path)
12
+
13
+ width = 350
14
+ height = 150
15
+ font_size = 32
16
+
17
+
18
+ def sample_text(font: DSFont) -> str:
19
+ if font.language == "ja":
20
+ return "こんにちは、世界\nフォント識別\nHello, world"
21
+ if font.language == "ko":
22
+ return "안녕하세요, 세계\n글꼴 인식하기\nHello, world"
23
+ if font.language == "zh-Hans":
24
+ return "你好,世界\n字体识别\nHello, world"
25
+ if font.language == "zh-Hant":
26
+ return "你好,世界\n字型辨識\nHello, world"
27
+ return "CJK字体\nCJKフォント\nCJK 글꼴"
28
+
29
+
30
+ for i, font in tqdm(enumerate(font_list)):
31
+ img = Image.new("RGB", (width, height), (255, 255, 255))
32
+ font_obj = ImageFont.truetype(font.path, font_size)
33
+ draw = ImageDraw.Draw(img)
34
+ text_width, text_height = draw.textsize(
35
+ sample_text(font),
36
+ font=font_obj,
37
+ language=None if font.language == "CJK" else font.language,
38
+ )
39
+ x = (width - text_width) // 2
40
+ y = (height - text_height) // 2
41
+ draw.text((x, y), sample_text(font), font=font_obj, fill=(0, 0, 0))
42
+ img.save(f"./demo_fonts/{i}.jpg")