all repos — python-meme-bot @ 5aa518d8efa435488f717af7d2f34a4036c7ec1d

Telegram Bot that uses PIL to compute light image processing.

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    text = textwrap.wrap(input_text, width=LINE_WIDTH)
175    if text == []:
176        return
177    
178    font = ImageFont.truetype(font=ARIAL_FONT_FILE, size=int(FONT_BASE))
179    d = ImageDraw.Draw(img)
180    
181    txt_height = (k2 * FONT_BASE - k3 + LINE_SPACING)
182    max_text_height = txt_height * len(text)
183    y = (img_height - max_text_height) / 2
184        
185    for i in range(len(text)):
186        txt_width = d.textbbox((0, 0), text[i], font=font)[2]
187        x = (img_width - txt_width - (len(text[i]) * LETTER_SPACING))/2
188
189        _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)
190
191        y += txt_height + LINE_SPACING
192        
193    img = img.resize((int(BASE_WIDTH/2), int(float(img.size[1]) * (BASE_WIDTH/2) / img.size[0])))
194    
195    if img.mode in ("RGBA", "P"):
196        img = img.convert("RGB")
197    
198    return img
199
200def text_effect(text: str, img: Image):
201    LETTER_SPACING = 1
202    LINE_SPACING = 3
203    STROKE_WIDTH = 1
204    STROKE_FILL = (0, 0, 0)
205    FONT_BASE = 75
206    MARGIN = 10
207    LINE_WIDTH = 20
208    
209    img = img.resize((BASE_WIDTH, int(img.size[1] * float(BASE_WIDTH / img.size[0]))))
210    
211    text = textwrap.wrap(text, width=LINE_WIDTH)
212    if text == []:
213        return
214
215    font = ImageFont.truetype(font=ARIAL_FONT_FILE, size=FONT_BASE)
216    
217    img_width, img_height = img.size
218    d = ImageDraw.Draw(img)
219    
220    _, _, max_txt_width, txt_height = d.textbbox((0, 0), text[0], font=font)
221    
222    for line in text:
223        temp = int(font.getlength(line))
224        if temp > max_txt_width:
225            max_txt_width = temp
226    
227    
228    total_height = (txt_height + LINE_SPACING) * len(text)
229    
230    y_inf = 0
231    y_sup = img_height - total_height
232    x_inf = 0
233    x_sup = img_width - max_txt_width - 5
234    
235    fill = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
236    x = random.randint(x_inf, x_sup)
237    y = random.randint(y_inf, y_sup)
238    for i in range(len(text)):
239        _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)
240
241        y += txt_height + LINE_SPACING
242        
243    img = img.resize((int(BASE_WIDTH/2), int(float(img.size[1]) * (BASE_WIDTH/2) / img.size[0])))
244    
245    if img.mode in ("RGBA", "P"):
246        img = img.convert("RGB")
247    
248    return img
249
250def test_multiple(text, effect, modifier=""):
251        imgs = os.listdir("test")
252        for i in range(len(imgs)):
253            image = effect(text, Image.open(os.path.join("test", imgs[i])))
254            image.save(os.path.join("test_output", f'output{modifier}{i}.jpg'), optimize=True, quality=80)
255
256        print("Image test successful")
257
258def test(text, effect, modifier=""):
259        image = effect(text, Image.open("image.jpg"))
260        image.save('output.jpg', optimize=True, quality=80)
261
262        print("Image test successful")
263
264def main():
265    input_text = '''
266Caught my gf pooping...so I broke up with her
267She 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
268'''
269    
270    #test(input_text, wot_effect)
271    test_multiple(input_text, wot_effect)
272    #test_multiple(input_text, splash_effect, "_long")
273    
274if __name__ ==  "__main__":
275    main()