all repos — python-meme-bot @ ee18ea77890feb2d14eb445366f8c6c3e07d5b9f

Telegram Bot that uses PIL to compute light image processing.

clean up code
Marco Andronaco andronacomarco@gmail.com
Wed, 07 Sep 2022 00:12:19 +0200
commit

ee18ea77890feb2d14eb445366f8c6c3e07d5b9f

parent

d6840de4407d0edc3d794313bb3a1bde777e745d

2 files changed, 33 insertions(+), 43 deletions(-)

jump to
M Effects.pyEffects.py

@@ -84,42 +84,6 @@ FONT_BASE = 75

MARGIN = 10 LINE_WIDTH = 20 - def _draw_splash(text, img): - font_first = ImageFont.truetype(font=ARIAL_FONT_FILE, size=FONT_FIRST) - font_base = ImageFont.truetype(font=ARIAL_FONT_FILE, size=FONT_BASE) - - img_width, img_height = img.size - - d = ImageDraw.Draw(img) - - _, _, first_txt_width, first_txt_height = d.textbbox((0, 0), text[0], font=font_first) - _, _, max_txt_width, txt_height = d.textbbox((0, 0), text[1], font=font_base) - - total_height = (txt_height + LINE_SPACING) * (len(text) - 1) + LINE_SPACING + first_txt_height - y = (img_height - total_height) / 2 - - for i in range(1, len(text)): - #temp = d.textbbox((0, 0), text[i], font=font_base)[2] - temp = int(font_base.getlength(text[i])) - if temp > max_txt_width: - max_txt_width = temp - - max_txt_width = max_txt_width if max_txt_width > first_txt_width else first_txt_width - x_start = (img_width - max_txt_width) / 2 - - for i in range(len(text)): - line = text[i] - x = x_start - ''' - if align == "center": - txt_width = d.textbbox((0, 0), line, font=font)[2] - x = (img_width - txt_width - (len(line) * LETTER_SPACING))/2 - ''' - font = font_base if i > 0 else font_first - _draw_line(d, x, y, line, font, LETTER_SPACING, FILL, STROKE_WIDTH, STROKE_FILL) - - y += (txt_height if i > 0 else first_txt_height) + LINE_SPACING - lines = [x for x in text.split("\n") if x] if len(lines) < 2: return img

@@ -128,11 +92,40 @@ img = img.resize((BASE_WIDTH, int(img.size[1] * float(BASE_WIDTH / img.size[0]))))

img = _darken_image(img) - split_text = textwrap.wrap(lines[1].upper(), width=LINE_WIDTH) - if split_text == []: + text = textwrap.wrap(lines[1].upper(), width=LINE_WIDTH) + if text == []: return - split_text.insert(0, lines[0]) - _draw_splash(split_text, img) + text.insert(0, lines[0]) + font_first = ImageFont.truetype(font=ARIAL_FONT_FILE, size=FONT_FIRST) + font_base = ImageFont.truetype(font=ARIAL_FONT_FILE, size=FONT_BASE) + + img_width, img_height = img.size + d = ImageDraw.Draw(img) + + _, _, first_txt_width, first_txt_height = d.textbbox((0, 0), text[0], font=font_first) + _, _, max_txt_width, txt_height = d.textbbox((0, 0), text[1], font=font_base) + + total_height = (txt_height + LINE_SPACING) * (len(text) - 1) + LINE_SPACING + first_txt_height + y = (img_height - total_height) / 2 + + for i in range(1, len(text)): + temp = int(font_base.getlength(text[i])) + if temp > max_txt_width: + max_txt_width = temp + + max_txt_width = max_txt_width if max_txt_width > first_txt_width else first_txt_width + x_start = (img_width - max_txt_width) / 2 + + for i in range(len(text)): + ''' + if align == "center": + txt_width = d.textbbox((0, 0), line, font=font)[2] + x = (img_width - txt_width - (len(line) * LETTER_SPACING))/2 + ''' + font = font_base if i > 0 else font_first + _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) + + y += (txt_height if i > 0 else first_txt_height) + LINE_SPACING img = img.resize((int(BASE_WIDTH/2), int(float(img.size[1]) * (BASE_WIDTH/2) / img.size[0])))
M main.pymain.py

@@ -40,9 +40,6 @@ image, markup = _get_image(context, bio=False)

image = _img_to_bio(tt_bt_effect(text, image)) return image, markup -def _parse_message(text: str): - pass - def _get_message_content(message): image = None if len(message.photo) > 0: