-
Notifications
You must be signed in to change notification settings - Fork 0
/
badge_generator.py
145 lines (121 loc) · 6.65 KB
/
badge_generator.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import cv2
import numpy as np
from PIL import ImageFont, Image, ImageDraw
import argparse
def export_string_badge(left_text: str,
right_text: str,
font_path: str,
font_size: int,
save_name: str,
horizontal_padding: int,
vertical_padding: int,
text_color: tuple,
left_background_color: tuple,
right_background_color: tuple) -> None:
"""
Exports a badge image to the current directory, constructed using the provided parameters.
:param left_string: the string for the left of the badge
:param right_string: the string for the right of the badge
:param font_path: the path to the true-type font to use for the painted strings
:param font_size: the size for the font
:param save_name: the name to save the png as
:param horizontal_padding: the left/right padding between the image borders and words
:param vertical_padding: the top/bottom padding between the image borders and words
:param text_color: the color for the text painted by left_string and right_string as a bgr tuple
:param left_background_color: the color used for the badge's left background as a bgr tuple
:param right_background_color: the color used for the badge's right background as a bgr tuple
"""
local_font = ImageFont.truetype(font_path, font_size)
left_width = get_text_size(left_text, font_size, font_path)[0]
right_width_height = get_text_size(right_text, font_size, font_path)
beta_width = right_width_height[0]
text_height = right_width_height[1]
full_width = (horizontal_padding + left_width
+ 2 * horizontal_padding + beta_width + horizontal_padding)
full_height = vertical_padding + text_height + vertical_padding
blank_image = np.zeros((full_height, full_width, 3), np.uint8)
alpha_color_drawn = cv2.rectangle(blank_image, (0, 0),
(left_width + 2 *
horizontal_padding, full_height),
left_background_color, -1)
beta_color_drawn = cv2.rectangle(alpha_color_drawn, (left_width + horizontal_padding, 0),
(full_width, full_height), right_background_color, -1)
base_colors_done = Image.fromarray(beta_color_drawn)
draw = ImageDraw.Draw(base_colors_done)
left_anchor = (horizontal_padding / 2, vertical_padding)
draw.text(left_anchor, left_text, font=local_font, fill=text_color)
draw = ImageDraw.Draw(base_colors_done)
left_anchor = (left_width + horizontal_padding * 2 + horizontal_padding / 2, vertical_padding)
draw.text(left_anchor, right_text, font=local_font, fill=text_color)
cv2.imwrite(save_name + '.png', np.array(base_colors_done))
def get_text_size(text: str, font_size: int, font_path: str) -> tuple:
"""
Returns a tuple of the size (width, height) required to hold the provided
string with the provided font and point size.
:param text: the text to return the width and height of
:param font_size: the size of the true-type font
:param font_name: the path to the true-type font file
:return: a tuple representing the width and height
"""
font = ImageFont.truetype(font_path, font_size)
bounding_box = font.getbbox(text)
return bounding_box[2], bounding_box[3]
def reverse_rgb_tuple_to_bgr(rgb_string: str) -> tuple:
"""
Parses an rgb tuple from the provided string of the format: "r,g,b" and returns a bgr tuple.
:param rgb_string: the rgb string in the form: "r,g,b"
:return: a tuple representing a bgr color
"""
parts = [int(part.strip()) for part in rgb_string.split(",")]
if len(parts) != 3:
raise Exception(f"Invalid color parts length: {len(parts)}, provided string: \"{rgb_string}\"")
return parts[2], parts[1], parts[0]
def main():
parser = argparse.ArgumentParser(prog='Badge Generator', description="Generates colorful and pretty badges with text")
parser.add_argument('-lt', '--left_text',
required=True,
help='the left text of the badge')
parser.add_argument('-rt', '--right_text',
required=True,
help='the right text of the badge')
parser.add_argument('-fp', '--font_path',
default="./fonts/oswald-semi-bold.ttf",
help='the relative or absolute path to the true-type font to use for the badge text')
parser.add_argument('-ts', '--text_size',
default='26',
help='the size of the text')
parser.add_argument('-sn', '--save_name',
help='the name to save the png badge as')
parser.add_argument('-hp', '--horizontal_padding',
default=15,
help='the horizontal padding of the text and badge edges')
parser.add_argument('-vp', '--vertical_padding',
default=10,
help='the vertical padding of the text and badge edges')
parser.add_argument('-tc', '--text_color',
default='245,245,245',
help='the color of the text to use in the format "r,g,b"')
parser.add_argument('-lbc', '--left_background_color',
default='5,83,131',
help='the color of the text to use in the format "r,g,b"')
parser.add_argument('-rbc', '--right_background_color',
default='85,147,199',
help='the color of the text to use in the format "r,g,b"')
args = parser.parse_args()
if args.save_name is None:
args.save_name = f"{args.left_text}_{args.right_text}"
text_color = reverse_rgb_tuple_to_bgr(args.text_color)
left_background_color = reverse_rgb_tuple_to_bgr(args.left_background_color)
right_background_color = reverse_rgb_tuple_to_bgr(args.right_background_color)
export_string_badge(left_text=args.left_text,
right_text=args.right_text,
font_path=args.font_path,
font_size=int(args.text_size),
save_name=args.save_name,
horizontal_padding=int(args.horizontal_padding),
vertical_padding=int(args.vertical_padding),
text_color=text_color,
left_background_color=left_background_color,
right_background_color=right_background_color)
if __name__ == '__main__':
main()