-
Notifications
You must be signed in to change notification settings - Fork 0
/
certgen.py
83 lines (57 loc) · 2.59 KB
/
certgen.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
# Certgen: Mass Certificate Generator
# https://github.com/TNI-Cybersec/Certgen
import os
from PIL import Image, ImageFont, ImageDraw
# Settings
FONT_FILE = r"fonts/BigShouldersInlineText/BigShouldersInlineText-Black.ttf"
FONT_FILE_2 = r"fonts/CormorantGaramond/CormorantGaramond-Bold.ttf"
FONT_COLOR = "#001848"
TEMPLATE = Image.open(r"templates/test.png")
WIDTH, HEIGHT = TEMPLATE.size
ACTIVITY_NAME = "ACTIVITY NAME"
DATE = "31-12-2023"
def gen_certificate(name: str) -> None:
"""Generate a certificate as a .png file"""
image_source = TEMPLATE.convert("RGB")
draw = ImageDraw.Draw(image_source)
# Drawing the text on the image.
draw_text(draw, ACTIVITY_NAME, adjh=-270, font=FONT_FILE_2, font_size=100)
draw_text(draw, DATE, adjw=-360, adjh=490, font=FONT_FILE, font_size=45)
draw_text(draw, name, adjh=50, font=FONT_FILE, font_size=90)
# Saving the certificates.
save_img(image_source, name)
def draw_text(draw: ImageDraw, text: str, font: str, adjw: int = 0, adjh: int = 0, font_size: int = 100) -> None:
"""Draw the text on the image"""
font_file = get_font(font, font_size)
name_width, name_height = get_textsize(draw, text, font=font_file)
draw.text(((WIDTH - name_width) / 2 + adjw, (HEIGHT - name_height) / 2 + adjh),
text, fill=FONT_COLOR, font=font_file)
def get_textsize(draw: ImageDraw, text: str, font: ImageFont) -> tuple[int, int]:
"""Return a tuple of (width, height) of the text"""
left, top, right, bottom = draw.textbbox((0, 0), text, font=font)
width = right - left
height = bottom - top
return width, height
def get_font(font_path: str, font_size: int) -> ImageFont:
"""Return a font object"""
return ImageFont.truetype(font_path, font_size)
def save_img(img: Image, name: str) -> None:
"""Save the image as a .png file"""
save_path = f"output/{ACTIVITY_NAME}"
if not os.path.exists(save_path):
os.makedirs(save_path)
img.save(f"{save_path}/{name}.png")
if __name__ == "__main__":
# Get names from a text file and convert to a list, one name per line.
names = open("names.txt", "r").read().split("\n")
# names = ["Zhong Xina", "Lucas Services", "Never Gonna Give You Up"]
# Remove empty line from names.
while ("" in names):
names.remove("")
names_length = len(names)
# Generate certificates for each name.
for i, name in enumerate(names):
print(f"[{i + 1}/{names_length}] Generating certificate of: {name}")
gen_certificate(name)
print('-' * os.get_terminal_size().columns)
print(f"All {names_length} certificates done!")