/
offer.py
177 lines (156 loc) · 7.33 KB
/
offer.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from roundedRect import roundedRect
class Offer:
def __init__(self, db, template, is_spotlight):
sf = 4
self.sf = sf
self.noto = db.installFont('assets/NotoSans-Regular.ttf')
self.recoleta = db.installFont('assets/Latinotype - Recoleta Light.otf')
# self.recoleta = db.installFont('assets/Latinotype - Recoleta Regular.otf')
self.recoleta_bold = db.installFont('assets/Latinotype - Recoleta Medium.otf')
self.db = db
self.content = template['content']
self.content['fontSize'] *= sf
button = template['button']
self.button = { 'fontSize': button['fontSize'] * sf, 'width': button['width'] * sf, 'height': button['height'] * sf, 'borderRadius': button['borderRadius'] * sf}
self.width = template['dimensions']['width'] * sf
self.height = template['dimensions']['height'] * sf
self.is_spotlight = is_spotlight
if self.is_spotlight:
self.spotlight = template['spotlight']
else:
self.spotlight = template['circle']
self.spotlight['d'] *= sf
self.spotlight['x'] *= sf
self.spotlight['y'] *= sf
self.logo = template['logo']
self.logo['x'] *= sf
self.logo['y'] *= sf
self.db.stroke(None)
self.db.blendMode('normal')
self.margin = self.width * .075
if (self.height < self.width):
self.margin = self.height * .075
def renderCopy(self, cursor):
self.db.fill(1)
self.db.blendMode('normal')
self.db.font(self.recoleta, self.content['fontSize'])
x = self.content['textbox_offer']['copy']['x'] * self.sf
y = self.content['textbox_offer']['copy']['y'] * self.sf
w = self.content['textbox_offer']['copy']['width'] * self.sf
h = self.content['textbox_offer']['copy']['height'] * self.sf
# copy_y = cursor - (self.margin / 1.5) - self.content['textbox_offer']['height']
self.db.textBox(self.copy, (x, y, w, h), align=self.content['align'])
def renderOffer(self):
self.db.stroke(1)
self.db.strokeWidth(4)
self.db.blendMode('normal')
self.db.fill(None)
x = self.content['textbox_offer']['box']['x'] * self.sf
y = self.content['textbox_offer']['box']['y'] * self.sf
w = self.content['textbox_offer']['box']['width'] * self.sf
h = self.content['textbox_offer']['box']['height'] * self.sf
if self.content['textbox_offer']['box']['outline'] > 0:
self.db.rect(x, y, w, h)
self.db.stroke(None)
self.db.fill(1)
self.db.font(self.recoleta_bold, self.content['textbox_offer']['box']['fontsize'] * self.sf)
margin = h / 4
if self.content['align'] == 'center' or self.content['textbox_offer']['box']['outline'] == 0:
margin = 0
self.db.textBox(self.offer, (x + margin, y - h / 5, w, h), align=self.content['align'])
def renderButton(self):
self.db.blendMode('normal')
self.switchFill('pink')
if self.color_scheme == 'pink':
self.switchFill('blue')
x = self.content['textbox_offer']['cta']['x'] * self.sf
y = self.content['textbox_offer']['cta']['y'] * self.sf
self.db.stroke(0,0,0,.1)
self.db.strokeWidth(1)
roundedRect(self.db, x, y, self.button['width'], self.button['height'], self.button['borderRadius'])
self.db.stroke(None)
self.db.font('Noto Sans')
self.db.fontSize(self.button['fontSize'])
self.db.fill(1) # product pink
self.db.textBox(self.cta, (x, y - self.button['height'] / 4, self.button['width'], self.button['height']), align="center")
def renderBadge(self):
badge = self.db.ImageObject('assets/badge.png')
badge_size = self.db.imageSize(badge)[0]
sf = self.content['textbox_offer']['badge']['width'] * self.sf / badge_size
badge.lanczosScaleTransform(sf)
# y = self.db.imageSize(badge)[1] + self.margin / 1.5
# x = self.margin
x = self.content['textbox_offer']['badge']['x'] * self.sf
y = self.content['textbox_offer']['badge']['y'] * self.sf
self.db.blendMode('normal')
self.db.image(badge, (x, y))
def renderLogo(self):
logo = self.db.ImageObject('assets/logo_mark_light_gray.png')
logo_size = self.db.imageSize(logo)[0]
sf = self.spotlight['d'] / logo_size
logo.lanczosScaleTransform(sf)
self.db.blendMode('multiply')
self.db.image(logo, (self.logo['x'], self.logo['y']))
def switchFill(self, colorName):
if (colorName == 'pink'):
self.db.fill(213/255,44/255,130/255) # product pink
elif (colorName == 'blue'):
self.db.fill(1/255,96/255,135/255) # product blue
else:
self.db.fill(35/255,53/255,75/255) # product slate
def renderFrame(self, frame_path):
self.db.blendMode('multiply')
if self.is_spotlight:
frame = self.db.ImageObject('assets/{}.png'.format(frame_path))
frame_size = self.db.imageSize(frame)[0]
sf = self.width / frame_size
frame.lanczosScaleTransform(sf)
self.db.image(frame, (0,0))
else:
self.switchFill(self.color_scheme)
self.db.rect(0,0,self.width, self.height)
self.db.fill(.85)
self.db.oval(self.spotlight['x'], self.spotlight['y'], self.spotlight['d'], self.spotlight['d'])
def renderPortrait(self, face, magic):
img = { 'path': face['path'], 'w': face['img']['w'], 'h': face['img']['h'] }
face = face['face']
self.db.fill(1,1,1,1)
self.db.rect(0,0,self.width, self.height)
im = self.db.ImageObject(img['path'])
im.photoEffectMono()
# mask = self.db.ImageObject('assets/mask_fade.png')
# im.blendWithMask(backgroundImage=None, maskImage=mask)
#scale the portrait to fit the spotlight
sf = self.spotlight['d'] / (face['w'] / img['w'] * self.db.imageSize(im)[0]) * magic
im.lanczosScaleTransform(sf)
# shift the image placement by the difference between where the face starts and where the spotlight is located
im_x = self.spotlight['x'] - round(face['x'] / img['w'] * self.db.imageSize(im)[0]) # this line of code took me four hours
im_y = self.spotlight['y'] - ( img['h'] - face['h'] - face['y'] ) / img['h'] * self.db.imageSize(im)[1] # this line of code took me two hours
self.db.image(im, (im_x, im_y))
def render(self, frame_path, face, copy, cta, offer):
self.copy = copy
self.offer = offer
self.cta = cta
if (frame_path.find('_a') > -1):
self.color_scheme = 'pink'
elif (frame_path.find('_b') > -1):
self.color_scheme = 'blue'
else:
self.color_scheme = 'slate'
self.db.newDrawing()
self.db.size(self.width, self.height)
if self.is_spotlight:
self.renderPortrait(face, .65)
self.renderFrame(frame_path)
self.renderLogo()
if self.copy is not None:
self.renderCopy(self.height)
self.renderBadge()
if (self.cta is not None):
self.renderButton()
if (self.offer is not None):
self.renderOffer()
def save(self, fp):
self.db.saveImage(fp)
def end(self):
self.db.endDrawing()