-
Notifications
You must be signed in to change notification settings - Fork 0
/
quotesmaker.py
289 lines (255 loc) · 12.5 KB
/
quotesmaker.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env python3
try:
from PIL import Image, ImageDraw, ImageFont
import argparse, os
import uuid
import textwrap
import random
import time
except ImportError:
print("[-] Please install requirements.txt by running \n pip install -r requirements.txt")
try:
def get_arguments():
ret_data1 = {
"quote": "",
"author": " ",
"template": None,
"logo": "template/0.png",
"type": 1
}
ret_data2 = {
"file": "",
"separator": "```",
"template": None,
"logo": "template/0.png",
"type": 2
}
parser = argparse.ArgumentParser(description="Quote maker by Aditya Giri\n either provide quote manually")
parser.add_argument("-q", "--quote", type=str, help="To manually provide quote")
parser.add_argument("-a", "--author", type=str,
help="To manually provide Author name.\n It will remain empty if not provided")
parser.add_argument("-t", "--template", type=int,
help="To select template between 1 and 4.\n Random will be selected for each quote if not provided.\n works for both mannual quote and quotes imported from file.")
parser.add_argument("-f", "--file", type=str,
help="To manually provide file name to convert to quote.\n -s or --separator can be provided if -f used default separator is (```)")
parser.add_argument("-s", "--separator", type=str,
help="default separator is (```) \nprovide separator exists between quote and author\n check readme.md for more info on using file as input")
parser.add_argument("-l", "--logo", type=str,
help="[!] Only .png file supported\nTo add logo watermark supports transparency(optional)")
data = parser.parse_args()
if not data.quote and not data.file:
print(
"[!]Qoutemaker Error:001\n(code to check in readme.md)\n[-] Please Provide either file name or quote \n see [-h] for more info")
exit()
elif data.quote and data.file:
print(
"[!]Qoutemaker Error:002\n(code to check in readme.md)\n[-] [-f] and [-q] are not allowed at a time \n see [-h] for more info")
exit()
elif data.quote and not data.file:
ret_data1["quote"] = data.quote
if data.author:
ret_data1["author"] = data.author
if data.template:
if data.template <= 0 or data.template > 7:
print("[-] Please Choose template between 1 - 7")
exit()
else:
ret_data1["template"] = int(data.template)
if data.logo:
if data.logo[-3:] != "png":
print(
"[!] Quotemaker Error:003\n(code to check in readme.md)\n[-] Currently This program only supports .png as logo watermark")
exit()
if os.path.exists(data.logo) == True:
ret_data1["logo"] = data.logo
if os.path.exists(data.logo) == False:
print(
"[!] Quotemaker Error:004\n(code to check in readme.md)\n[-] Specified {} doesn't exists Please Check and try again".format(
data.logo))
exit()
return ret_data1
elif data.file and not data.quote:
if os.path.exists(data.file) == True:
ret_data2["file"] = data.file
if os.path.exists(data.file) == False:
print(
"[!] Quotemaker Error:005\n(code to check in readme.md)\n[-] Specified File {} doesn't exists Please Check and try again".format(
data.file))
exit()
if data.separator:
ret_data2["separator"] = data.separator
if data.logo:
if data.logo[-3:] != "png":
print(
"[!] Quotemaker Error:003\n(code to check in readme.md)\n[-] Currently This program only supports .png as logo watermark")
exit()
if os.path.exists(data.logo) == True:
ret_data2["logo"] = data.logo
if os.path.exists(data.logo) == False:
print(
"[!] Quotemaker Error:004\n(code to check in readme.md)\n[-] Specified {} doesn't exists Please Check and try again".format(
data.logo))
exit()
if data.template:
if data.template <= 0 or data.template > 7:
print("[-] Please Choose template between 1 - 7")
exit()
else:
ret_data2["template"] = int(data.template)
return ret_data2
def random_dat(typed):
if typed == 0:
return random.randint(1, 7)
elif typed == 1:
return str(uuid.uuid4().hex)
def wrap(text, wd):
wrapper = textwrap.TextWrapper(width=wd)
data = wrapper.wrap(text)
data1 = '\n'.join(data)
return data1
def get_temp(num, quote, author, sz, wd, n, logo, f_num="1.otf"):
back_img = Image.open("template/" + str(num) + ".png")
logo = Image.open(logo)
new_width = int(.1 * back_img.width)
logo.thumbnail((new_width, new_width))
x = int(back_img.width * .8)
y = int(back_img.height * .1) - int(logo.height / 2)
back_img.paste(logo, (x, y), logo)
canvas = Image.new("RGBA", back_img.size)
draw = ImageDraw.Draw(canvas)
font = ImageFont.truetype("font/font" + str(f_num), size=sz)
format_quote = wrap(quote, wd)
text_width, text_height = draw.textsize(format_quote, font)
if num == 7:
location = (back_img.width / 2 - text_width / 2, int(back_img.height * .6) - text_height / 2 - 18)
elif num == 3:
location = (back_img.width / 2 - text_width / 2, int(back_img.height * .55) - text_height / 2 - 18)
else:
location = (back_img.width / 2 - text_width / 2, back_img.height / 2 - text_height / 2 - 18)
draw.text(location, format_quote, font=font, fill=(255, 255, 255, 255))
text_width1, text_height1 = draw.textsize(author, font)
if num == 1:
crd_location = (.05 * back_img.width, back_img.height - .1 * back_img.height)
elif num == 2:
crd_location = (int(back_img.width * .7) - text_width1 / 2,
back_img.height - int(back_img.height * .1) - text_height1 / 2 - 18)
elif num == 7:
crd_location = (int(back_img.width * .52) - text_width1 / 2,
back_img.height - int(back_img.height * .05) - text_height1 / 2 - 18)
else:
crd_location = (int(back_img.width * .37) - text_width1 / 2,
back_img.height - int(back_img.height * .1) - text_height1 / 2 - 18)
font1 = ImageFont.truetype("font/font" + str(f_num), size=35)
if num != 7 :
draw.text(crd_location, author, font=font1, fill=(255, 255, 255, 255))
else:
draw.text(crd_location, author.upper(), font=font1, fill=(0, 0, 0, 255))
back_img.paste(canvas, canvas)
NEW = back_img.convert("RGB")
NEW.save("Quotes/" + str(n) + ".jpg", quality=100)
def temp1(quote, author, sz, wd, n, logo):
get_temp(1, quote, author, sz, wd, n, logo, "1.otf")
def temp2(quote, author, sz, wd, n, logo):
get_temp(2, quote, author, sz, wd, n, logo, "3.otf")
def temp3(quote, author, sz, wd, n, logo):
get_temp(3, quote, author, sz, wd, n, logo, "3.otf")
def temp4(quote, author, sz, wd, n, logo):
get_temp(4, quote, author, sz, wd, n, logo, "3.otf")
def temp5(quote, author, sz, wd, n, logo):
get_temp(5, quote, author, sz, wd, n, logo, "3.otf")
def temp6(quote, author, sz, wd, n, logo):
get_temp(6, quote, author, sz, wd, n, logo, "3.otf")
def temp7(quote, author, sz, wd, n, logo):
get_temp(7, quote, author, sz, wd, n, logo, "2.ttf")
def quotesmaker(quote, author, n, t_num, logo):
if t_num <= 6:
if len(quote) < 100:
sz = 50
wd = 27
if len(quote) >= 100 and len(quote) <= 250:
sz = 40
wd = 30
if len(quote) > 250 and len(quote) >= 300:
sz = 30
wd = 34
if len(quote) > 300:
sz = 25
wd = 50
if t_num == 7:
if len(quote) < 100:
sz = 50
wd = 24
if len(quote) >= 100 and len(quote) <= 250:
sz = 40
wd = 30
if len(quote) > 250 and len(quote) >= 300:
sz = 28
wd = 37
if len(quote) > 300:
sz = 25
wd = 50
if t_num == 1:
temp1(quote, author, sz, wd, n, logo)
if t_num == 2:
temp2(quote, author, sz, wd, n, logo)
if t_num == 3:
temp3(quote, author, sz, wd, n, logo)
if t_num == 4:
temp4(quote, author, sz, wd, n, logo)
if t_num == 5:
temp5(quote, author, sz, wd, n, logo)
if t_num == 6:
temp6(quote, author, sz, wd, n, logo)
if t_num == 7:
temp7(quote, author, sz, wd, n, logo)
def condition_analysis(quote_data):
if quote_data:
if quote_data["type"] == 1:
if quote_data["template"] == None:
file_name = random_dat(1)
quotesmaker(quote_data["quote"], quote_data["author"], file_name, random_dat(0), quote_data["logo"])
print(
"[+] Image has been successfully saved in Quotes/{}.jpg\n [#] You can request more template in our github page".format(
file_name))
else:
file_name = random_dat(1)
quotesmaker(quote_data["quote"], quote_data["author"], file_name, quote_data["template"],
quote_data["logo"])
print(
"[+] Image has been successfully saved in Quotes/{}.jpg\n [#] You can request more template in our github page".format(
file_name))
elif quote_data["type"] == 2:
quotes_file = open(quote_data["file"], "r").read().splitlines()
if quote_data["template"] == None:
for n in range(len(quotes_file)):
t_num = random_dat(0)
t = n + 1
quotesmaker(quotes_file[n].split(quote_data["separator"])[0],
quotes_file[n].split(quote_data["separator"])[1], t, t_num, quote_data["logo"])
print(
"[+] Image has been successfully saved in Quotes/1-{}.jpg\n [#] You can request more template in our github page".format(
t))
else:
for n in range(len(quotes_file)):
t = n + 1
quotesmaker(quotes_file[n].split(quote_data["separator"])[0],
quotes_file[n].split(quote_data["separator"])[1], t, quote_data["template"],
quote_data["logo"])
print(
"[+] Image has been successfully saved in Quotes/1-{}.jpg\n [#] You can request more template in our github page".format(
t))
try:
os.mkdir('Quotes')
except FileExistsError:
pass
quote_data = get_arguments()
condition_analysis(quote_data)
except Exception:
if Exception == ValueError:
print("[-] Error encountered\n Your logo file is either corrupt or not supported")
elif Exception == KeyboardInterrupt:
print("[-] ctrl+c detected exiting")
exit()
else:
print(Exception)
print("[-]Error encountered Try waiting some time\n If the error repeats report us at github")