-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcarnet.py
140 lines (112 loc) · 3.38 KB
/
carnet.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
from __future__ import print_function
from PIL import Image, ImageFont, ImageDraw
class Carnet:
_bg_img_path = 'data/front-bg.png'
_font_path = 'data/DejaVuSans-Bold.ttf'
_employee = None
NAME_MAX_LENGHT = 18
DEPARTMENT_MAX_LENGHT = 30
POSITION_MAX_LENGHT = 18
def __init__(self, emp=None):
self._employee = emp
def get_emp_photo(self):
if self._employee:
return 'output/%s.jpg' % self._employee.pid
def create(self):
imbg = Image.open(self._bg_img_path)
imph = Image.open('output/pics/%s.jpg' % self._employee.pid)
carnet_file = "output/carnets/%s.png" % self._employee.pid
dpmt = self._employee.department.split("\n")
empos = self._employee.position.split("\n")
draw = ImageDraw.Draw(imbg)
font = ImageFont.truetype(self._font_path, size=18)
font2 = ImageFont.truetype(self._font_path, size=18)
font3 = ImageFont.truetype(self._font_path, size=22)
line_size = 20
empos_line_size = 28
idnum_xpos = 173
idnum_ypos = 302
name_xpos = 173
name_ypos = 165
surname_xpos = 173
surname_ypos = 232
dpto_xpos = 20
dpto_ypos = 340
position_xpos = 20
position_ypos = 615
draw.text(
(idnum_xpos, idnum_ypos + line_size * 0),
str(self._employee.pid),
(0, 0, 0),
font=font
)
draw.text(
(name_xpos, name_ypos + line_size * 0),
self._employee.name,
(0, 0, 0),
font=font
)
draw.text(
(name_xpos, name_ypos + line_size * 1),
self._employee.sname,
(0, 0, 0),
font=font
)
draw.text(
(surname_xpos, surname_ypos + line_size * 0),
self._employee.surname,
(0, 0, 0),
font=font
)
draw.text(
(surname_xpos, surname_ypos + line_size * 1),
self._employee.ssurname,
(0, 0, 0),
font=font
)
draw.text(
(dpto_xpos, dpto_ypos + line_size * 0),
dpmt[0],
(0, 0, 0),
font=font2
)
draw.text(
(dpto_xpos, dpto_ypos + line_size * 1),
dpmt[1],
(0, 0, 0),
font=font2
)
draw.text(
(dpto_xpos, dpto_ypos + line_size * 2),
dpmt[2],
(0, 0, 0),
font=font2
)
draw.text(
(position_xpos, position_ypos + empos_line_size * 0),
empos[0],
(255, 255, 255),
font=font3
)
draw.text(
(position_xpos, position_ypos + empos_line_size * 1),
empos[1],
(255, 255, 255),
font=font3
)
photo_x1crop = 140
photo_y1crop = 0
photo_x2crop = 500
photo_y2crop = 480
photo_xpos = 20
photo_ypos = 140
photo_size = (145, 193)
box = (photo_x1crop, photo_y1crop, photo_x2crop, photo_y2crop)
region = imph.crop(box)
region = region.resize(photo_size)
imbg.paste(region, (photo_xpos, photo_ypos))
imbg.save(carnet_file, 'PNG')
if __name__ == "__main__":
from employee import Employee
c = Carnet(Employee(18084850))
print(c.create())