-
Notifications
You must be signed in to change notification settings - Fork 1
/
certificate_generator.py
94 lines (84 loc) · 3.27 KB
/
certificate_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
from PIL import Image, ImageDraw, ImageFont
import configparser
class Certificate:
"""
Defines an certificate with the following properties:
Attributes:
name: A string representing the certificate reciever's name.
course: A string representing the name of the course enrolled in.
date: A string representing the date of issue of certificate.
output: A string representing the name of the output file that will be generated (has to be an image file).
"""
def __init__(self,name,course,date,output):
"""
Creates and returns an instace of certificate with the attributes specified
"""
self.name = name
self.course = course
self.date = date
self.output = output
def configure(self):
"""
Configures the settings and properties necessary to create the certificate.
This function imports the configurations from the "config.txt" file.
The different configuration options are discussed in the documentation
"""
config = configparser.ConfigParser()
try:
config.read("config.txt")
self.cert = config['SETTINGS']['TEMPLATE']
self.font1 = config['NAME']['NAME_FONT']
self.color1 = config['NAME']['NAME_COLOR']
self.size1 = int(config['NAME']['NAME_FONT_SIZE'])
self.size2 = int(config['EVENT']['EVENT_FONT_SIZE'])
self.font2 = config['EVENT']['EVENT_FONT']
self.color2 = config['EVENT']['EVENT_COLOR']
self.size3 = int(config['DATE']['DATE_FONT_SIZE'])
self.font3 = config['DATE']['DATE_FONT']
self.color3 = config['DATE']['DATE_COLOR']
self.x1 = int(config['NAME']['NAME_X'])
self.y1 = int(config['NAME']['NAME_y'])
self.width1 = int(config['NAME']['NAME_WIDTH'])
self.x2 = int(config['EVENT']['EVENT_X'])
self.y2 = int(config['EVENT']['EVENT_y'])
self.width2 = int(config['EVENT']['EVENT_WIDTH'])
self.x3 = int(config['DATE']['DATE_X'])
self.y3 = int(config['DATE']['DATE_Y'])
self.width3 = int(config['DATE']['DATE_WIDTH'])
except:
print("\n!! ERORR : CHECK THE CONFIG FILE !!\n")
return 0
return 1
def create(self):
"""
Creates the actual certificate with the configurations and inputs specified
and saves the output accordingly.
"""
if self.configure()==0:
return 0
try:
image = Image.open(self.cert)
except:
print("\n!! ERROR : CHECK TEMPLATE FILE !!\n")
return 0
draw = ImageDraw.Draw(image)
try:
font1 = ImageFont.truetype(self.font1, size=self.size1)
font2 = ImageFont.truetype(self.font2, size=self.size2)
font3 = ImageFont.truetype(self.font3, size=self.size3)
except:
print("\n!! ERROR : CHECK FONT FILES !!\n")
return 0
width = font1.getsize(self.name)[0]
draw.text((self.x1+(self.width1-width)/2, self.y1-self.size1), self.name, fill=self.color1, font=font1)
width = font2.getsize(self.course)[0]
draw.text((self.x2+(self.width2-width)/2, self.y2-self.size2), self.course, fill=self.color2, font=font2)
width = font3.getsize(self.date)[0]
draw.text((self.x3+(self.width3-width)/2, self.y3-self.size3), self.date, fill=self.color3, font=font3)
try:
image.save(self.output)
print("\n>> CERTIFICATE GENERATED AND SAVED AS : "+self.output+ " <<\n")
except:
print("\n!! ERORR : CERTIFICATE COULD NOT BE SAVED !!\n")
return 0
return 1