-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcertificate.py
48 lines (46 loc) · 1.68 KB
/
certificate.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
from PIL import Image, ImageDraw, ImageFont
# ==== COLOUR DICTIONARY WITH HEX COLOUR VALUE
colour_dict = {
'Black': '#000000',
'White': '#FFFFFF',
'Red': '#FF0000',
'Lime': '#00FF00',
'Blue': '#FFFF00',
'Yellow': '#FFFF00'
}
# =============================================== PREVIEW CERTIFICATE =================================================
def previewcertificate(Path, fontstyle, Colour, fontsize, coord):
# === CONVERT COORD STRING INTO INT TYPE (X , Y)
splitcoord = coord.split(",")
x = splitcoord[0]
y = splitcoord[1]
location = (int(x), int(y))
# === FONT STYLE & FONT SIZE
font = ImageFont.truetype(fontstyle, int(fontsize))
# === OPEN TEMPLATE
image = Image.open(Path)
draw = ImageDraw.Draw(image)
# === EMBEDDED NAME
draw.text(location, 'Sample Name', fill=colour_dict[Colour], font=font)
# === SAVE IMAGE FILE
image.save('sample_image.jpeg')
# =============================================== CREATE CERTIFICATE =================================================
def createcertificate(Name, Path, fontstyle, Colour, fontsize, coord):
# === CONVERT COORD STRING INTO INT TYPE (X , Y)
splitcoord = coord.split(",")
x = splitcoord[0]
y = splitcoord[1]
location = (int(x), int(y))
# === FONT STYLE & FONT SIZE
font = ImageFont.truetype(fontstyle, int(fontsize))
# === OPEN TEMPLATE
image = Image.open(Path)
draw = ImageDraw.Draw(image)
# === EMBEDDED NAME
draw.text(location, Name, fill=colour_dict[Colour], font=font)
# === IMAGE NAME VAR (FIRSTNAME_LASTNAME.JPEG)
image_name = Name.replace(' ', '_') + ".jpeg"
# === SAVE IMAGE FILE
image.save(image_name)
# === RETURN IMAGE FILE NAME
return image_name