-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.py3
207 lines (173 loc) · 7.42 KB
/
generator.py3
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
from os import path
from os import listdir
import sys
from PIL import Image, ImageDraw, ImageFont
### >>> INIT <<< ###
class Variables(object):
textContainerHeight = 0
leftIcon = None
rightIcon = None
imgFraction = 0.5
### >>> FUNCTIONS <<< ###
# Function to change the icon's size
# To adapt it to source image
def changeImageSizeWithRatio(background: Image, overlay: Image, isLeftIcon: bool, centerIcon: bool):
backgroundWidth = background.size[0]
backgroundHeight = background.size[1]
overlayWidth = overlay.size[0]
overlayHeight = overlay.size[1]
if (isLeftIcon and centerIcon):
fraction = 1
else:
fraction = 0.33
if overlayWidth > overlayHeight:
width = backgroundWidth * fraction
widthPercent = (width / float(overlayWidth))
height = int((float(overlayHeight) * float(widthPercent)))
else:
height = backgroundHeight * fraction
heightPercent = (height / float(overlayHeight))
width = int((float(overlayWidth) * float(heightPercent)))
return overlay.resize((int(width), int(height)))
# Add icon(s) to the source image
def customizeImage(backgroundPath: str, leftIconPath: str, rightIconPath: str, outputPath: str, textColor: str, centerIcon: bool):
background = Image.open(backgroundPath)
background = background.convert("RGBA")
backgroundWidth = background.size[0]
backgroundHeight = background.size[1]
if path.exists(leftIconPath):
Variables.leftIcon = Image.open(leftIconPath)
Variables.leftIcon = Variables.leftIcon.convert("RGBA")
else:
Variables.leftIcon = createAnImageFromText(backgroundPath, leftIconPath, textColor, centerIcon)
Variables.leftIcon = Variables.leftIcon.convert("RGBA")
if path.exists(rightIconPath):
Variables.rightIcon = Image.open(rightIconPath)
Variables.rightIcon = Variables.rightIcon.convert("RGBA")
else:
Variables.rightIcon = createAnImageFromText(backgroundPath, rightIconPath, textColor, centerIcon)
Variables.rightIcon = Variables.rightIcon.convert("RGBA")
result = background.copy()
if Variables.leftIcon != None:
Variables.leftIcon = changeImageSizeWithRatio(background, Variables.leftIcon, True, centerIcon)
leftIconWidth = Variables.leftIcon.size[0]
leftIconHeight = Variables.leftIcon.size[1]
x = getXPosition(backgroundWidth, leftIconWidth, True, centerIcon)
y = getYPosition(backgroundHeight, leftIconHeight, True, centerIcon)
result.paste(Variables.leftIcon, (x, y), Variables.leftIcon)
if Variables.rightIcon != None:
Variables.rightIcon = changeImageSizeWithRatio(background, Variables.rightIcon, False, centerIcon)
rightIconWidth = Variables.rightIcon.size[0]
rightIconHeight = Variables.rightIcon.size[1]
x = getXPosition(backgroundWidth, rightIconWidth, False, centerIcon)
y = getYPosition(backgroundHeight, rightIconHeight, False, centerIcon)
result.paste(Variables.rightIcon, (x, y), Variables.rightIcon)
result.save(outputPath, quality=100)
# To add icon(s) to each item (function triggered only if the source is a folder)
def findAndCustomizeImages(basepath: str, leftIconPath: str, rightIconPath: str, textColor: str, centerIcon: bool):
for fileName in listdir(basepath):
fullPath = basepath + fileName
if not path.exists(fullPath):
print("!! Image not found, please check the path )")
print("!! Path: " + fullPath)
sys.exit(1)
if path.isfile(fullPath) and (fullPath.endswith('.png') or fullPath.endswith('.jpg') or fullPath.endswith('.jpeg')):
customizeImage(fullPath, leftIconPath, rightIconPath, fullPath, textColor, centerIcon)
# Convert the input text to image
def createAnImageFromText(backgroundPath: str, text: str, textColor: str, centerIcon: bool):
background = Image.open(backgroundPath)
background = background.convert("RGBA")
backgroundWidth = background.size[0]
backgroundHeight = background.size[1]
fontsize = 1
txtFraction = Variables.imgFraction - 0.02
iconFont = ImageFont.truetype(stepRootPath + "/Arial-Bold.ttf", fontsize)
while iconFont.getsize(text)[0] < (txtFraction * backgroundWidth):
# iterate until the text size is just larger than the criteria
fontsize += 1
iconFont = ImageFont.truetype(stepRootPath + "/Arial-Bold.ttf", fontsize)
Variables.textContainerHeight = iconFont.getsize(text)[1]
if (centerIcon):
iconBackgroundColor=(192, 192, 192, 120)
txtImageWidth = backgroundWidth
txtImageHeight = backgroundHeight
else:
iconBackgroundColor=(0, 0, 0, 0)
txtImageWidth = iconFont.getsize(text)[0]
txtImageHeight = Variables.textContainerHeight
iconBackground = Image.new(mode="RGBA", size=(txtImageWidth, txtImageHeight), color=iconBackgroundColor)
textContainer = ImageDraw.Draw(iconBackground)
# text_width, text_height = textContainer.textsize(text, font=iconFont)
# center texte in container
x_pos = int((txtImageWidth - iconFont.getsize(text)[0])/2)
y_pos = int((txtImageHeight - iconFont.getsize(text)[1])/2)
textContainer.text((x_pos, y_pos), text, font=iconFont, fill=textColor)
return iconBackground
# Helpers
def getXPosition(backgroundWidth: int, iconWidth: int, isLeftIcon: bool, centerIcon: bool):
if (isLeftIcon == True):
if (centerIcon):
return int(backgroundWidth/ 2 - iconWidth/ 2)
else:
return int((backgroundWidth/ 2 - iconWidth)/ 2)
else:
if (centerIcon == True):
return int(backgroundWidth/ 2)
else:
return int(backgroundWidth/ 2 + (backgroundWidth/ 2 - iconWidth) / 2)
def getYPosition(backgroundHeight: int, iconHeight: int, isLeftIcon: bool, centerIcon: bool):
if (centerIcon):
if (isLeftIcon):
return int(backgroundHeight/2 - iconHeight/ 2)
else:
if path.exists(leftIconPath):
return int(backgroundHeight/2)
else:
# if we display a text, to not hide it
return int(backgroundHeight/2 + (int(Variables.textContainerHeight * Variables.imgFraction))/ 2)
else:
return int(backgroundHeight - iconHeight - backgroundHeight/10)
### >>>> MAIN <<<< ###
if (len(sys.argv) < 3) or (not sys.argv[2]):
print("!! Empty background (first parameter)")
sys.exit(1)
if len(sys.argv) < 4:
print("!! Script required at least 2 parameters (source_image, left_icon and/or right_icon)")
sys.exit(1)
stepRootPath = sys.argv[1]
backgroundPath = sys.argv[2]
print('backgroundPath : ', backgroundPath)
if not path.exists(backgroundPath):
print("!! Background image not found, please check the path )")
print("!! Path: " + backgroundPath)
sys.exit(1)
if len(sys.argv) > 5 and len(sys.argv[5]):
outputPath = sys.argv[5]
else:
outputPath = backgroundPath
print('outputPath : ', outputPath)
if len(sys.argv) > 6 and len(sys.argv[6]):
textColor = sys.argv[6]
else:
textColor = "#FFFFFF"
print('textColor : ', textColor)
if len(sys.argv) > 7 and len(sys.argv[7]):
centerIcon = sys.argv[7] == "True"
else:
centerIcon = False
print('centerIcon : ', centerIcon)
if (centerIcon):
Variables.imgFraction = 0.6
if(len(sys.argv[3])):
leftIconPath = sys.argv[3]
print('leftIconPath : ', leftIconPath)
if((len(sys.argv) > 4) and (len(sys.argv[4]))):
rightIconPath = sys.argv[4]
print('rightIconPath : ', rightIconPath)
elif not leftIconPath:
print("!! Script required at least 2 parameters (source_image, left_icon and/or right_icon)")
sys.exit(1)
if path.isfile(backgroundPath):
customizeImage(backgroundPath, leftIconPath, rightIconPath, outputPath, textColor, centerIcon)
else:
findAndCustomizeImages(backgroundPath, leftIconPath, rightIconPath, textColor, centerIcon)