Effects.py (view raw)
1from PIL import Image, ImageDraw, ImageFont, ImageEnhance
2import textwrap, os, random, time, math
3
4random.seed(time.time())
5
6BASE_WIDTH = 1200
7IMPACT_FONT_FILE = os.path.join("fonts", "impact.ttf")
8ARIAL_FONT_FILE = os.path.join("fonts", "opensans.ttf")
9
10def _darken_image(image: Image, amount=0.5):
11 return ImageEnhance.Brightness(image).enhance(amount)
12
13def _draw_line(d: ImageDraw, x: int, y: int, line: str, font: ImageFont, letter_spacing: int = 9, fill = (255, 255, 255), stroke_width: int = 9, stroke_fill = (0, 0, 0)):
14
15 for i in range(len(line)):
16 d.text((x, y), line[i], fill=fill, stroke_width=stroke_width, font=font, stroke_fill=stroke_fill)
17 x += font.getlength(line[i]) + letter_spacing
18
19def tt_bt_effect(text: str, img: Image):
20 LETTER_SPACING = 9
21 LINE_SPACING = 10
22 FILL = (255, 255, 255)
23 STROKE_WIDTH = 9
24 STROKE_FILL = (0, 0, 0)
25 FONT_BASE = 100
26 MARGIN = 10
27
28 def _draw_tt_bt(text, img, bottom=False):
29 split_caption = textwrap.wrap(text.upper(), width=20)
30 if split_caption == []:
31 return
32 font_size = FONT_BASE + 10 if len(split_caption) <= 1 else FONT_BASE
33 font = ImageFont.truetype(font=IMPACT_FONT_FILE, size=font_size)
34 img_width, img_height = img.size
35
36 d = ImageDraw.Draw(img)
37 txt_height = d.textbbox((0, 0), split_caption[0], font=font)[3]
38
39 if bottom:
40 factor = -1
41 split_caption.reverse()
42 y = (img_height - (img_height / MARGIN)) - (txt_height / 2)
43 else:
44 factor = 1
45 y = (img_height / MARGIN) - (txt_height / 1.5)
46
47 for line in split_caption:
48 txt_width = d.textbbox((0, 0), line, font=font)[2]
49
50 x = (img_width - txt_width - (len(line) * LETTER_SPACING)) / 2
51
52 _draw_line(d, x, y, line, font, LETTER_SPACING, FILL, STROKE_WIDTH, STROKE_FILL)
53
54 y += (txt_height + LINE_SPACING) * factor
55
56 lines = [x for x in text.split("\n") if x]
57
58 tt = lines[0] if len(lines) > 0 else None
59 bt = lines[1] if len(lines) > 1 else None
60
61 img = img.resize((BASE_WIDTH, int(img.size[1] * float(BASE_WIDTH / img.size[0]))))
62
63 if tt is None and bt is None:
64 return img
65
66 if (tt is not None):
67 _draw_tt_bt(tt, img)
68 if (bt is not None):
69 _draw_tt_bt(bt, img, bottom=True)
70
71 img = img.resize((int(BASE_WIDTH / 2), int(float(img.size[1]) * (BASE_WIDTH / 2) / img.size[0])))
72
73 if img.mode in ("RGBA", "P"):
74 img = img.convert("RGB")
75
76 return img
77
78def splash_effect(text: str, img: Image):
79 LETTER_SPACING = 1
80 LINE_SPACING = 3
81 FILL = (255, 255, 255)
82 STROKE_WIDTH = 1
83 STROKE_FILL = (0, 0, 0)
84 FONT_FIRST = 50
85 FONT_BASE = 75
86 MARGIN = 10
87 LINE_WIDTH = 20
88
89 lines = [x for x in text.split("\n") if x]
90 first_line = lines.pop(0)
91 text = "\n".join(lines)
92
93 img = img.resize((BASE_WIDTH, int(img.size[1] * float(BASE_WIDTH / img.size[0]))))
94
95 img = _darken_image(img)
96
97 text = textwrap.wrap(text.upper(), width=LINE_WIDTH)
98 if text == []:
99 return
100 text.insert(0, first_line)
101
102 while True:
103 font_first = ImageFont.truetype(font=ARIAL_FONT_FILE, size=int(FONT_BASE - (FONT_BASE / 2)))
104 font_base = ImageFont.truetype(font=ARIAL_FONT_FILE, size=FONT_BASE)
105
106 img_width, img_height = img.size
107 d = ImageDraw.Draw(img)
108
109 _, _, first_txt_width, first_txt_height = d.textbbox((0, 0), text[0], font=font_first)
110 _, _, max_txt_width, txt_height = d.textbbox((0, 0), text[1], font=font_base)
111
112 total_height = (txt_height + LINE_SPACING) * (len(text) - 1) + LINE_SPACING + first_txt_height
113
114 if (total_height < img_height / 2) or (FONT_BASE < 10):
115 break
116
117 FONT_BASE = FONT_BASE - 5
118
119 y = (img_height - total_height) / 2
120
121 for i in range(1, len(text)):
122 temp = int(font_base.getlength(text[i]))
123 if temp > max_txt_width:
124 max_txt_width = temp
125
126 max_txt_width = max_txt_width if max_txt_width > first_txt_width else first_txt_width
127 x_start = (img_width - max_txt_width) / 2
128
129 for i in range(len(text)):
130 '''
131 if align == "center":
132 txt_width = d.textbbox((0, 0), line, font=font)[2]
133 x = (img_width - txt_width - (len(line) * LETTER_SPACING)) / 2
134 '''
135 font = font_base if i > 0 else font_first
136 _draw_line(d=d, x=x_start, y=y, line=text[i], font=font, letter_spacing=LETTER_SPACING, fill=FILL, stroke_width=STROKE_WIDTH, stroke_fill=STROKE_FILL)
137
138 y += (txt_height if i > 0 else first_txt_height) + LINE_SPACING
139
140 img = img.resize((int(BASE_WIDTH / 2), int(float(img.size[1]) * (BASE_WIDTH / 2) / img.size[0])))
141
142 if img.mode in ("RGBA", "P"):
143 img = img.convert("RGB")
144
145 return img
146
147def wot_effect(input_text: str, img: Image):
148 LETTER_SPACING = 1
149 LINE_SPACING = 3
150 FILL = (255, 255, 255)
151 STROKE_WIDTH = 1
152 STROKE_FILL = (0, 0, 0)
153 FONT_BASE = 10
154
155 img = img.resize((BASE_WIDTH, int(img.size[1] * float(BASE_WIDTH / img.size[0]))))
156 img = _darken_image(img)
157
158 img_width, img_height = img.size
159
160 MARGIN_H = img_height / 4
161 MARGIN_W = 0
162
163 w = img_width - MARGIN_W
164 h = img_height - MARGIN_H
165 n = len(input_text.strip())
166 k1 = 0.612123
167 k2 = 1.216428
168 k3 = 0.341428
169 k4 = 0.364576
170
171 FONT_BASE = (math.sqrt(4 * k1 * k2 * h * n * w + math.pow(k2, 2) * math.pow(n, 2) * math.pow(LETTER_SPACING, 2) + ((2 * k1 * k2 * k3 - 2 * k4 * math.pow(k2, 2)) * math.pow(n, 2) - 2 * k1 * k2 * LINE_SPACING) * LETTER_SPACING + math.pow(k1, 2) * math.pow(n, 2) * math.pow(LINE_SPACING, 2) + (2 * k1 * k4 * k2 - 2 * math.pow(k1, 2) * k3) * math.pow(n, 2) * LINE_SPACING + (math.pow(k1, 2) * math.pow(k3, 2) - 2 * k1 * k4 * k2 * k3 + math.pow(k4, 2) * math.pow(k2, 2)) * math.pow(n, 2)) - k2 * n * LETTER_SPACING - k1 * n * LINE_SPACING + (k1 * k3 + k4 * k2) * n) / (2 * k1 * k2 * n)
172 LINE_WIDTH = w / (k1 * FONT_BASE - k4 + LETTER_SPACING)
173
174 if len(input_text) > LINE_WIDTH:
175 text = textwrap.wrap(input_text.strip(), width=LINE_WIDTH)
176 else:
177 text = [input_text]
178 if text == []:
179 return
180
181 font = ImageFont.truetype(font=ARIAL_FONT_FILE, size=int(FONT_BASE))
182 d = ImageDraw.Draw(img)
183
184 txt_height = (k2 * FONT_BASE - k3 + LINE_SPACING)
185 max_text_height = txt_height * len(text)
186 y = (img_height - max_text_height) / 2
187
188 for i in range(len(text)):
189 txt_width = d.textbbox((0, 0), text[i], font=font)[2]
190 x = (img_width - txt_width - (len(text[i]) * LETTER_SPACING)) / 2
191
192 _draw_line(d=d, x=x, y=y, line=text[i], font=font, letter_spacing=LETTER_SPACING, fill=FILL, stroke_width=STROKE_WIDTH, stroke_fill=STROKE_FILL)
193
194 y += txt_height + LINE_SPACING
195
196 img = img.resize((int(BASE_WIDTH / 2), int(float(img.size[1]) * (BASE_WIDTH / 2) / img.size[0])))
197
198 if img.mode in ("RGBA", "P"):
199 img = img.convert("RGB")
200
201 return img
202
203def text_effect(text: str, img: Image):
204 LETTER_SPACING = 1
205 LINE_SPACING = 3
206 STROKE_WIDTH = 1
207 STROKE_FILL = (0, 0, 0)
208 FONT_BASE = 75
209 MARGIN = 10
210 LINE_WIDTH = 20
211
212 img = img.resize((BASE_WIDTH, int(img.size[1] * float(BASE_WIDTH / img.size[0]))))
213
214 text = textwrap.wrap(text, width=LINE_WIDTH)
215 if text == []:
216 return
217
218 font = ImageFont.truetype(font=ARIAL_FONT_FILE, size=FONT_BASE)
219
220 img_width, img_height = img.size
221 d = ImageDraw.Draw(img)
222
223 _, _, max_txt_width, txt_height = d.textbbox((0, 0), text[0], font=font)
224
225 for line in text:
226 temp = int(font.getlength(line))
227 if temp > max_txt_width:
228 max_txt_width = temp
229
230
231 total_height = (txt_height + LINE_SPACING) * len(text)
232
233 y_inf = 0
234 y_sup = img_height - total_height
235 x_inf = 0
236 x_sup = img_width - max_txt_width - 5
237
238 fill = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
239 x = random.randint(x_inf, x_sup)
240 y = random.randint(y_inf, y_sup)
241 for i in range(len(text)):
242 _draw_line(d=d, x=x, y=y, line=text[i], font=font, letter_spacing=LETTER_SPACING, fill=fill, stroke_width=STROKE_WIDTH, stroke_fill=STROKE_FILL)
243
244 y += txt_height + LINE_SPACING
245
246 img = img.resize((int(BASE_WIDTH / 2), int(float(img.size[1]) * (BASE_WIDTH / 2) / img.size[0])))
247
248 if img.mode in ("RGBA", "P"):
249 img = img.convert("RGB")
250
251 return img
252
253def test_multiple(text, effect, modifier=""):
254 imgs = os.listdir("test")
255 for i in range(len(imgs)):
256 image = effect(text, Image.open(os.path.join("test", imgs[i])))
257 image.save(os.path.join("test_output", f'output{modifier}{i}.jpg'), optimize=True, quality=80)
258
259 print("Image test successful")
260
261def test(text, effect, modifier=""):
262 image = effect(text, Image.open("image.jpg"))
263 image.save('output.jpg', optimize=True, quality=80)
264
265 print("Image test successful")
266
267def main():
268 input_text = '''
269Caught my gf pooping...so I broke up with her
270She said shes off to pee while were watching a movie, now shes been gone 5 minutes and i knew something was up, i knocked on the door and asked if everything is ok, she said yes she'll be right out...her voice was labored and i became suspicious...so i yelled "IM COMING IN!' she screamed no but there was no stopping this, i smashed through the door and i see her sitting on the toilet seat, i told her to get the fuk up, she didnt so i threw her off, i looked inside the toilet...just as i suspected, a goddam log, bitch u better pray this isnt yours. i looked around and saw no pet in site, I KNOW THIS IS UR POOP U WHORE, she screamed at me that im crazy and that shes calling the cops, all the while toilet paper in her hands. i told her no need to call the cops, im breaking up with u u some kinda poop whore. and that was that. I feel like a new man and off to find a woman who doesnt poop
271'''
272
273 #test(input_text, wot_effect)
274 test_multiple(input_text, wot_effect)
275 #test_multiple(input_text, splash_effect, "_long")
276
277if __name__ == "__main__":
278 main()