diff --git a/config.json b/config.json index 5c24fe1..300cc0f 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,7 @@ { "safe_mode": false, "loglevel": "info", + "show_test_result_images": true, "text": { "font": "fonts/Futura_Condensed_Extra_Bold.otf", diff --git a/src/config.py b/src/config.py index 23f7e3b..e2871b2 100644 --- a/src/config.py +++ b/src/config.py @@ -34,6 +34,7 @@ "error": logging.ERROR, } loglevel = __logdict[config["loglevel"]] +show_test_result_images = config["show_test_result_images"] pngcrush_enabled = config["optimization"]["pngcrush"]["enabled"] diff --git a/src/generatecaption.py b/src/generatecaption.py index 065d24a..1f416f5 100644 --- a/src/generatecaption.py +++ b/src/generatecaption.py @@ -85,13 +85,12 @@ def generate_caption_image(rawtext: str) -> Image.Image: config.bg_color, ) # paste merged lines in the middle - caption.paste( + caption.alpha_composite( merged_lines, ( (caption.width - merged_lines.width) // 2, (caption.height - merged_lines.height) // 2, - ), - merged_lines, + ) ) logging.debug(f"Added padding to caption, size: {caption.size}") diff --git a/tests/test_generatecaption.py b/tests/test_generatecaption.py new file mode 100644 index 0000000..d7674c0 --- /dev/null +++ b/tests/test_generatecaption.py @@ -0,0 +1,51 @@ +import unittest +import sys +from PIL import Image +from src.generatecaption import generate_caption_image +from src import config + + +class TestGenerateCaption(unittest.TestCase): + show = config.show_test_result_images + def test_generate_caption_image_hu(self): + rawtext = "Árvíztűrő tükörfúrógép" + result = generate_caption_image(rawtext) + self.assertIsInstance(result, Image.Image) + + if self.show: + result.show() + + def test_generate_caption_image_emoji(self): + rawtext = ":3 <3 :+1: :skull: 🦊 🐱" + result = generate_caption_image(rawtext) + self.assertIsInstance(result, Image.Image) + + if self.show: + result.show() + + def test_generate_caption_image_long(self): + rawtext = "faliure after " * 32 + result = generate_caption_image(rawtext) + self.assertIsInstance(result, Image.Image) + + if self.show: + result.show() + + def test_generate_caption_image_emote(self): + rawtext = "<:floraSmug:1112288234488201307> " * 40 + result = generate_caption_image(rawtext) + self.assertIsInstance(result, Image.Image) + + if self.show: + result.show() + + def test_generate_caption_image_longword(self): + rawtext = "test_generate_caption_image_longword_" + "A" * 100 + result = generate_caption_image(rawtext) + self.assertIsInstance(result, Image.Image) + + if self.show: + result.show() + +if __name__ == "__main__": + unittest.main()