This repository has been archived by the owner on Jul 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
category.py
61 lines (50 loc) · 1.81 KB
/
category.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
from pathlib import Path
from PIL import Image, ImageDraw
from PIL.Image import Image as Img
from config import Config
from utils import BLACK, get_info, resized_img
class Category:
def __init__(self, path: Path, config: Config) -> None:
self.path = path
self.name = path.name
self.rgb, self.ko, self.en, self.symbol = get_info(path)
self.conf = config
self.img = Image.open(path)
self.canvas = self.render()
def render(self) -> Img:
canvas = Image.new("RGBA", self.conf.canvas_size, self.rgb)
txt = Image.new("RGBA", self.conf.canvas_size, self.rgb)
img = resized_img(self.img, 60)
draw = ImageDraw.Draw(txt)
draw.text(
xy=self.conf.per(self.conf.font_symbol_location),
text=self.symbol,
font=self.conf.font_symbol_ttf,
fill=(*self.fontcolor, 64),
)
draw.text(
xy=self.conf.per(self.conf.font_ko_location),
text=self.ko,
font=self.conf.font_ko_ttf
if len(self.en) < self.conf.font_ko_len_bound
else self.conf.font_ko_ttf_small,
fill=self.fontcolor,
)
draw.text(
xy=self.conf.per(self.conf.font_en_location),
text=self.en,
font=self.conf.font_en_ttf
if len(self.en) < self.conf.font_en_len_bound
else self.conf.font_en_ttf_small,
fill=self.fontcolor,
)
result = Image.alpha_composite(canvas, txt)
result.paste(img, self.conf.per(self.conf.image_location), img)
return result
def save(self):
self.canvas.save(f"dist/{self.name}")
@property
def fontcolor(self):
if all([br > 200 for br in self.rgb]):
return BLACK
return self.conf.font_color