-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate.py
executable file
·129 lines (109 loc) · 4.34 KB
/
generate.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
#!/usr/bin/env python
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
from urllib import urlretrieve
import os
import re
# Sample Data
pk = 7
name = u'Mary Louise Streep'
phone = u'9876543211'
devil_number = u'007'
# The co-ordinates
devil_number_ending_xy = (744, 68)
name_ending_xy = (1010, 438)
phone_ending_xy = (1010, 520)
qr_xy = (65, 393)
# The font-sizes
devil_number_size = 58
name_size = 60
phone_size = 43
#Configuration
base_image = 'watermarked.jpg'
draw_qr = False
# possible values: L, M, Q, H
qr_error_correction = 'M'
# Pre-process the name
name = name.upper()
names = name.split()
last_name = names[-1]
names_sans_last = names[0:-1]
if len(names_sans_last) > 1:
middle_names = names_sans_last[1:]
# multiple middle names support - Amrit Bahadur Khanal Kshetri :D
middle_name_initials_list = [middle_name[0]+'.' for middle_name in middle_names]
middle_name_initials = ' '.join(middle_name_initials_list)
name_sans_last = names_sans_last[0] + ' ' + middle_name_initials + ' '
else:
name_sans_last = names_sans_last[0] + ' '
# Pre-process the phone number
phone_code = None
pattern = '^([0|\\+[0-9]{1,5})?[-\s]?([7-9][0-9]{9})$'
matches = re.search(pattern, phone)
if matches:
phone_code = matches.groups()[0]
phone = matches.groups()[1]
if phone_code:
phone_code = phone_code.strip('\n\t\r\+\-')
phone_code = phone_code.lstrip('00')
else:
phone_code = '977'
phone_code = '+' + phone_code + ' '
img = Image.open(base_image)
# img = Image.open('watermarked_with_qr.jpg')
# img = Image.open('front.jpg')
draw = ImageDraw.Draw(img)
# write devil number
devil_number_font = ImageFont.truetype(os.path.join('fonts', 'Aileron-ThinItalic.otf'),
devil_number_size)
devil_number_text_size = draw.textsize('#' + devil_number, font=devil_number_font)
devil_number_xy = (devil_number_ending_xy[0] - devil_number_text_size[0], devil_number_ending_xy[1])
draw.text(devil_number_xy, '#' + devil_number, (255, 255, 255), font=devil_number_font)
# write name
name_sans_last_font = ImageFont.truetype(os.path.join('fonts', 'Aileron-ThinItalic.otf'), name_size)
name_sans_last_size = draw.textsize(name_sans_last, font=name_sans_last_font)
last_name_font = ImageFont.truetype(os.path.join('fonts', 'Aileron-Italic.otf'), name_size)
last_name_size = draw.textsize(last_name, last_name_font)
name_length = name_sans_last_size[0] + last_name_size[0]
name_xy = (name_ending_xy[0] - name_length, name_ending_xy[1])
last_name_xy = (name_xy[0] + name_sans_last_size[0], name_xy[1])
draw.text(name_xy, name_sans_last, (255, 255, 255), font=name_sans_last_font)
draw.text(last_name_xy, last_name, (255, 255, 255), font=last_name_font)
# write phone number
phone_font = ImageFont.truetype(os.path.join('fonts', 'Aileron-Italic.otf'),
phone_size)
phone_font_size = draw.textsize(phone, phone_font)
phone_xy = (phone_ending_xy[0] - phone_font_size[0], phone_ending_xy[1])
draw.text(phone_xy, phone, (255, 255, 255), font=phone_font)
if phone_code:
phone_code_font = ImageFont.truetype(os.path.join('fonts', 'Aileron-ThinItalic.otf'),
phone_size)
phone_code_size = draw.textsize(phone_code, phone_code_font)
phone_code_xy = (phone_xy[0] - phone_code_size[0], phone_ending_xy[1])
draw.text(phone_code_xy, phone_code, (255, 255, 255), font=phone_code_font)
if draw_qr:
# download qr
if not os.path.exists('qrs'):
os.makedirs('qrs')
urlretrieve(
'http://api.qrserver.com/v1/create-qr-code/?data=http://manutd.org.np/' + devil_number + '&size=208x208&ecc=' + qr_error_correction + '&color=ffffff&bgcolor=000',
os.path.join('qrs', str(pk) + '.png'))
qr = Image.open(os.path.join('qrs', str(pk) + '.png'))
#make qr transparent
qr = qr.convert('RGBA')
data = qr.getdata()
new_data = []
for item in data:
if item[0] == 0 and item[1] == 0 and item[2] == 0:
new_data.append((255, 255, 255, 0))
else:
new_data.append(item)
qr.putdata(new_data)
qr.save(os.path.join('qrs', str(pk) + '.png'))
# write qr to image
img = img.convert('RGBA')
img.paste(qr, qr_xy, qr)
if not os.path.exists('sample_cards'):
os.makedirs('sample_cards')
img.save(os.path.join('sample_cards', str(pk) + '.png'))