File size: 2,820 Bytes
c6905fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b49d997
c6905fb
 
 
 
 
 
 
b49d997
c6905fb
 
 
 
 
 
 
 
 
 
 
b49d997
c6905fb
 
 
b49d997
 
 
 
 
c6905fb
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path

import fire
from PIL import Image, ImageDraw, ImageFont

from utils import KanaData


class FontImageGenerator:
    def __init__(

        self,

        font_path="data/KiwiMaru-Regular.ttf",

        img_size=640,

        padding=100,

        y_offset=-200,

        font_color="#AAA",

        grid_width=15,

        grid_color="#EEE",

        grid_split=3,

        grid_bg_color="white",

        kana_data_path="data/kana-data.json",

        output_dir="data/images",

        bg_file_name="bg.png",

        char_dir_name="kana-chars",

    ):
        self.font_path = font_path
        self.output_dir = Path(output_dir)
        self.img_size = img_size
        self.y_offset = y_offset
        self.font_color = font_color
        self.grid_width = grid_width
        self.grid_color = grid_color
        self.grid_split = grid_split
        self.grid_bg_color = grid_bg_color

        self.kana_data = KanaData.load(kana_data_path)
        self.font = ImageFont.truetype(self.font_path, img_size - padding)

        self.bg_path = self.output_dir / bg_file_name
        self.char_dir = self.output_dir / char_dir_name
        self.char_dir.mkdir(parents=True, exist_ok=True)

    def generate(self):
        image, _ = self.create_grid()
        image.save(self.bg_path)

        for kana_char in self.kana_data.spell:
            if len(kana_char) == 1:
                self.create_font_image(kana_char)

    def create_font_image(self, char):
        image, draw = self.create_grid()
        bbox = draw.textbbox((0, 0), char, font=self.font)

        w = bbox[2] - bbox[0]
        h = bbox[3] - bbox[1]

        x = (self.img_size - w) / 2
        y = (self.img_size - h) / 2 + self.y_offset

        draw.text((x, y), char, font=self.font, fill=self.font_color)
        image.save(self.char_dir / f"{char}.png")

    def create_grid(self):
        image = Image.new("RGB", (self.img_size, self.img_size), self.grid_bg_color)
        draw = ImageDraw.Draw(image)

        ver_line = [(0, 0), (0, self.img_size)]
        hor_line = [(0, 0), (self.img_size, 0)]
        draw.line(ver_line, fill=self.grid_color, width=self.grid_width)
        draw.line(hor_line, fill=self.grid_color, width=self.grid_width)

        cell_size = self.img_size // self.grid_split
        for i in range(self.grid_split):
            point = (i + 1) * cell_size
            ver_line = [(point, 0), (point, self.img_size)]
            hor_line = [(0, point), (self.img_size, point)]
            draw.line(ver_line, fill=self.grid_color, width=self.grid_width)
            draw.line(hor_line, fill=self.grid_color, width=self.grid_width)

        return image, draw


if __name__ == "__main__":
    fire.Fire(FontImageGenerator)