-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimage.py
42 lines (36 loc) · 1.67 KB
/
image.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
from PIL import Image, ImageDraw, ImageFont
from typing import Tuple
class DrawText:
def __init__(self, image: ImageDraw.ImageDraw, font: str) -> None:
self._img = image
self._font = str(font)
def get_box(self, text: str, size: int):
return ImageFont.truetype(self._font, size).getbbox(text)
def draw(self,
pos_x: int,
pos_y: int,
size: int,
text: str,
color: Tuple[int, int, int, int] = (255, 255, 255, 255),
anchor: str = 'lt',
stroke_width: int = 0,
stroke_fill: Tuple[int, int, int, int] = (0, 0, 0, 0),
multiline: bool = False):
font = ImageFont.truetype(self._font, size)
if multiline:
self._img.multiline_text((pos_x, pos_y), str(text), color, font, anchor, stroke_width=stroke_width, stroke_fill=stroke_fill)
else:
self._img.text((pos_x, pos_y), str(text), color, font, anchor, stroke_width=stroke_width, stroke_fill=stroke_fill)
def draw_partial_opacity(self,
pos_x: int,
pos_y: int,
size: int,
text: str,
po: int = 2,
color: Tuple[int, int, int, int] = (255, 255, 255, 255),
anchor: str = 'lt',
stroke_width: int = 0,
stroke_fill: Tuple[int, int, int, int] = (0, 0, 0, 0)):
font = ImageFont.truetype(self._font, size)
self._img.text((pos_x + po, pos_y + po), str(text), (0, 0, 0, 128), font, anchor, stroke_width=stroke_width, stroke_fill=stroke_fill)
self._img.text((pos_x, pos_y), str(text), color, font, anchor, stroke_width=stroke_width, stroke_fill=stroke_fill)