all repos — python-meme-bot @ 182b3dc1816842ad31c62e1dd5d41663766166aa

Telegram Bot that uses PIL to compute light image processing.

added test for effects
Marco Andronaco andronacomarco@gmail.com
Mon, 05 Sep 2022 18:10:34 +0200
commit

182b3dc1816842ad31c62e1dd5d41663766166aa

parent

3fd391b693d6859ad5f0edfd1190cc400364261f

2 files changed, 58 insertions(+), 50 deletions(-)

jump to
M .gitignore.gitignore

@@ -3,4 +3,6 @@ venv

output_image.jpg __pycache__ .ipynb_checkpoints -output.jpg+output.jpg +test +test_output
M Effects.pyEffects.py

@@ -1,49 +1,53 @@

from PIL import Image, ImageDraw, ImageFont import textwrap, os -IMPACT_FONT_FILE = os.path.join("fonts", "impact.ttf") -BASE_WIDTH = 1200 -LETTER_SPACING = 9 -LINE_SPACING = 10 -FILL = (255, 255, 255) -STROKE_WIDTH = 9 -STROKE_FILL = (0, 0, 0) -FONT_BASE = 100 -MARGIN = 10 - -def _draw_tt_bt(text, img, bottom=False): - split_caption = textwrap.wrap(text.upper(), width=20) - if split_caption == []: - return - font_size = FONT_BASE + 10 if len(split_caption) <= 1 else FONT_BASE - font = ImageFont.truetype(font=IMPACT_FONT_FILE, size=font_size) - img_width, img_height = img.size - - d = ImageDraw.Draw(img) - txt_height = d.textbbox((0, 0), split_caption[0], font=font)[3] - - if bottom: - factor = -1 - split_caption.reverse() - y = (img_height - (img_height / MARGIN)) - (txt_height / 2) - else: - factor = 1 - y = ((img_height / MARGIN)) - (txt_height / 1.5) - - for line in split_caption: - txt_width = d.textbbox((0, 0), line, font=font)[2] - - x = (img_width - txt_width - (len(line) * LETTER_SPACING))/2 - for i in range(len(line)): - char = line[i] - width = font.getlength(char) - d.text((x, y), char, fill=FILL, stroke_width=STROKE_WIDTH, font=font, stroke_fill=STROKE_FILL) - x += width + LETTER_SPACING - y = y + (txt_height + LINE_SPACING) * factor + def tt_bt_effect(text, img): + IMPACT_FONT_FILE = os.path.join("fonts", "impact.ttf") + BASE_WIDTH = 1200 + LETTER_SPACING = 9 + LINE_SPACING = 10 + FILL = (255, 255, 255) + STROKE_WIDTH = 9 + STROKE_FILL = (0, 0, 0) + FONT_BASE = 100 + MARGIN = 10 + + def _draw_tt_bt(text, img, bottom=False): + split_caption = textwrap.wrap(text.upper(), width=20) + if split_caption == []: + return + font_size = FONT_BASE + 10 if len(split_caption) <= 1 else FONT_BASE + font = ImageFont.truetype(font=IMPACT_FONT_FILE, size=font_size) + img_width, img_height = img.size + + d = ImageDraw.Draw(img) + txt_height = d.textbbox((0, 0), split_caption[0], font=font)[3] + + if bottom: + factor = -1 + split_caption.reverse() + y = (img_height - (img_height / MARGIN)) - (txt_height / 2) + else: + factor = 1 + y = ((img_height / MARGIN)) - (txt_height / 1.5) + + for line in split_caption: + txt_width = d.textbbox((0, 0), line, font=font)[2] + + x = (img_width - txt_width - (len(line) * LETTER_SPACING))/2 + + for i in range(len(line)): + char = line[i] + width = font.getlength(char) + d.text((x, y), char, fill=FILL, stroke_width=STROKE_WIDTH, font=font, stroke_fill=STROKE_FILL) + x += width + LETTER_SPACING + + y = y + (txt_height + LINE_SPACING) * factor + lines = [x for x in text.split("\n") if x] tt = lines[0] if len(lines) > 0 else None

@@ -67,15 +71,17 @@ img = img.convert("RGB")

return img -def image_test(): - image = Image.open("image.jpg") - #image, url = get_random_image() - - res = tt_bt_effect("top text\nbottom text", image) - res.save('./output.jpg', optimize=True, quality=80) - - print("Image test successful") +def test(text, effect, modifier=""): + imgs = os.listdir("test") + for i in range(len(imgs)): + image = effect(text, Image.open(os.path.join("test", imgs[i]))) + image.save(os.path.join("test_output", f'output{modifier}{i}.jpg'), optimize=True, quality=80) + + print("Image test successful") + +def main(): + test("top text\nbottom text", tt_bt_effect) + test("top text top text top text top text top text\nbottom text bottom text bottom text bottom text bottom text", tt_bt_effect, "_long") if __name__ == "__main__": - #main() - image_test()+ main()