diff --git a/docs/build_docs.py b/docs/build_docs.py index 31c4d69..f83ffe1 100644 --- a/docs/build_docs.py +++ b/docs/build_docs.py @@ -5,21 +5,27 @@ from meme_otron import img_factory as imgf from meme_otron import meme_db from meme_otron import utils -logging.basicConfig(format="[%(asctime)s][%(levelname)s][%(module)s] %(message)s", level=logging.DEBUG) +logging.basicConfig(format="[%(asctime)s][%(levelname)s][%(module)s] %(message)s", level=logging.WARNING) imgf.load_fonts() meme_db.load_memes() dst_dir = utils.relative_path(__file__, "templates") +templates_dir = utils.relative_path(__file__, "..", "templates") + for f in os.listdir(dst_dir): if path.isfile(path.join(dst_dir, f)): os.unlink(path.join(dst_dir, f)) +count = 0 + for meme_id in meme_db.DATA: meme = meme_db.get_meme(meme_id) if meme is not None: img = imgf.make(meme.template, meme.texts, debug=True) if img is not None: img.save(path.join(dst_dir, meme.template)) - print(meme_id) + count += 1 + +print(f"{count} registered templates / {len(os.listdir(templates_dir))} files") \ No newline at end of file diff --git a/docs/templates/brain3.jpg b/docs/templates/brain3.jpg deleted file mode 100644 index 824fd4c..0000000 Binary files a/docs/templates/brain3.jpg and /dev/null differ diff --git a/docs/templates/brain4.jpg b/docs/templates/brain4.jpg deleted file mode 100644 index 9eeff5a..0000000 Binary files a/docs/templates/brain4.jpg and /dev/null differ diff --git a/docs/templates/brain5.jpg b/docs/templates/brain5.jpg deleted file mode 100644 index 8bcc234..0000000 Binary files a/docs/templates/brain5.jpg and /dev/null differ diff --git a/docs/templates/disappointed.jpg b/docs/templates/disappointed.jpg deleted file mode 100644 index d51959a..0000000 Binary files a/docs/templates/disappointed.jpg and /dev/null differ diff --git a/docs/templates/drake.jpg b/docs/templates/drake.jpg deleted file mode 100644 index 6f62d32..0000000 Binary files a/docs/templates/drake.jpg and /dev/null differ diff --git a/docs/templates/nope.jpg b/docs/templates/nope.jpg deleted file mode 100644 index 45db123..0000000 Binary files a/docs/templates/nope.jpg and /dev/null differ diff --git a/docs/templates/pleasure3.jpg b/docs/templates/pleasure3.jpg deleted file mode 100644 index ff5743c..0000000 Binary files a/docs/templates/pleasure3.jpg and /dev/null differ diff --git a/docs/templates/pleasure4.jpg b/docs/templates/pleasure4.jpg deleted file mode 100644 index f6ba669..0000000 Binary files a/docs/templates/pleasure4.jpg and /dev/null differ diff --git a/docs/templates/tough2.jpg b/docs/templates/tough2.jpg deleted file mode 100644 index 87352c4..0000000 Binary files a/docs/templates/tough2.jpg and /dev/null differ diff --git a/docs/templates/tough2bis.jpg b/docs/templates/tough2bis.jpg deleted file mode 100644 index fe1193e..0000000 Binary files a/docs/templates/tough2bis.jpg and /dev/null differ diff --git a/docs/templates/tough3.jpg b/docs/templates/tough3.jpg deleted file mode 100644 index 868708d..0000000 Binary files a/docs/templates/tough3.jpg and /dev/null differ diff --git a/docs/templates/winnie2.jpg b/docs/templates/winnie2.jpg deleted file mode 100644 index 983f73c..0000000 Binary files a/docs/templates/winnie2.jpg and /dev/null differ diff --git a/docs/templates/winnie3.jpg b/docs/templates/winnie3.jpg deleted file mode 100644 index c0c2fdc..0000000 Binary files a/docs/templates/winnie3.jpg and /dev/null differ diff --git a/fonts/grafo.ttf b/fonts/grafo.ttf new file mode 100644 index 0000000..f6d9873 Binary files /dev/null and b/fonts/grafo.ttf differ diff --git a/meme_otron/img_factory.py b/meme_otron/img_factory.py index ef1450b..6082177 100644 --- a/meme_otron/img_factory.py +++ b/meme_otron/img_factory.py @@ -131,18 +131,18 @@ def get_pos(size, text, font): pos_y = 0 text_size = font.getsize_multiline(text.text, stroke_width=text.stroke_width * font.size) - if text.position.value[0] == "S": + if int(text.position.value) // 3 == 0: pos_y = min_y - elif text.position.value[0] == "C": + elif int(text.position.value) // 3 == 1: pos_y = round((min_y + max_y) / 2 - text_size[1] / 2) - elif text.position.value[0] == "N": + else: pos_y = max_y - text_size[1] - if text.position.value[1] == "W": + if int(text.position.value) % 3 == 0: pos_x = min_x - elif text.position.value[1] == "C": + elif int(text.position.value) % 3 == 1: pos_x = round((min_x + max_x) / 2 - text_size[0] / 2) - elif text.position.value[1] == "E": + else: pos_x = max_x - text_size[0] return pos_x, pos_y diff --git a/meme_otron/meme_db.py b/meme_otron/meme_db.py index cda27ce..4d85cd4 100644 --- a/meme_otron/meme_db.py +++ b/meme_otron/meme_db.py @@ -96,7 +96,10 @@ def load_item(i, item): else: DATA[item_id] = Meme(item_id, aliases, abstract, template, font, font_size, texts) for alias in aliases: - ALIASES[alias] = item_id + if alias in ALIASES: + logger.warning(f"Item '{item_id}'({i}): alias '{alias}' already registered by '{ALIASES[alias]}'") + else: + ALIASES[alias] = item_id logger.info(f"Loaded meme '{item_id}' with {len(texts)} texts") except KeyError as e: logger.warning(f"Item '{item_id}'({i}): key {e} not found") @@ -142,7 +145,7 @@ def load_text(j, raw_text): if "fill" in raw_text: if not (utils.is_list_of(raw_text["fill"], [int], 3)): raise TypeError(f"'fill' is not a list of 3 int") - text.fill = raw_text["fill"] + text.fill = tuple(raw_text["fill"]) if "stroke_width" in raw_text: if not (isinstance(raw_text["stroke_width"], float)): raise TypeError(f"'stroke_width' is not a float") @@ -150,7 +153,7 @@ def load_text(j, raw_text): if "stroke_fill" in raw_text: if not (utils.is_list_of(raw_text["stroke_fill"], [int], 3)): raise TypeError(f"'stroke_fill' is not a list of 3 int") - text.stroke_fill = raw_text["stroke_fill"] + text.stroke_fill = tuple(raw_text["stroke_fill"]) if "align" in raw_text: if raw_text["align"] not in ["left", "center", "right"]: raise TypeError(f"'align' is not 'left', 'center' or 'right'") diff --git a/meme_otron/types.py b/meme_otron/types.py index 002dbe5..f7768af 100644 --- a/meme_otron/types.py +++ b/meme_otron/types.py @@ -1,20 +1,20 @@ -from enum import Enum +from enum import IntEnum import copy -class Pos(Enum): +class Pos(IntEnum): """ TODO """ - NW = "NW" - N = "NC" - NE = "NE" - W = "CW" - CENTER = "CC" - E = "CE" - SW = "NW" - S = "NC" - SE = "NE" + NW = 0 + N = 1 + NE = 2 + W = 3 + CENTER = 4 + E = 5 + SW = 6 + S = 7 + SE = 8 class Meme: @@ -51,12 +51,22 @@ class Text: def __init__(self, text=None): self.text = text + self.x_range = (0, 1) self.y_range = (0, 1) - self.position = Pos.CENTER + + self.font = None self.font_size = None + self.fill = (0, 0, 0) self.stroke_width = 0 self.stroke_fill = (0, 0, 0) - self.font = None + self.align = "center" + self.position = Pos.CENTER + + def update(self, base): + for prop in ["font", "font_size", "fill", "stroke_width", + "stroke_fill", "align", "position"]: + if getattr(self, prop) is None: + setattr(self,prop, getattr(base, prop)) diff --git a/memes.json b/memes.json index 2ffa41e..b9221c1 100644 --- a/memes.json +++ b/memes.json @@ -122,6 +122,24 @@ "x_range": [0.02, 0.48], "y_range": [0.80, 1.00] }] +},{ + "id": "2_texts", + "abstract": true, + "font": "impact", + "font_size": 0.12, + "texts": [{ + "x_range": [0.02, 0.98], + "y_range": [0.02, 0.50], + "stroke_fill": [255, 255, 255], + "stroke_width": 0.1, + "position": "N" + },{ + "x_range": [0.02, 0.98], + "y_range": [0.50, 0.98], + "stroke_fill": [255, 255, 255], + "stroke_width": 0.1, + "position": "S" + }] },{ "id": "drake", "template": "drake.jpg", @@ -180,4 +198,110 @@ "id": "winnie3", "template": "winnie3.jpg", "based_on": "3_panel_right" +},{ + "id": "burn", + "template": "burn.jpg", + "font": "arial", + "texts": [{ + "x_range": [0.08, 0.37], + "y_range": [0.09, 0.43] + },{ + "x_range": [0.65, 0.95], + "y_range": [0.21, 0.40], + "stroke_fill": [255, 255, 255], + "stroke_width": 0.1 + },{ + "x_range": [0.16, 0.45], + "y_range": [0.73, 0.91], + "stroke_fill": [255, 255, 255], + "stroke_width": 0.1 + },{ + "x_range": [0.69, 0.98], + "y_range": [0.72, 0.90], + "stroke_fill": [255, 255, 255], + "stroke_width": 0.1 + }] +},{ + "id": "aliens", + "template": "aliens.jpg", + "font": "impact", + "font_size": 0.15, + "texts": [{ + "x_range": [0.02, 0.98], + "y_range": [0.8, 0.98], + "stroke_fill": [255, 255, 255], + "stroke_width": 0.1, + "position": "S" + }] +},{ + "id": "grandma", + "template": "grandma.jpg", + "based_on": "2_texts" +},{ + "id": "everywhere", + "template": "everywhere.jpg", + "aliases": ["buzz", "woody"], + "based_on": "2_texts" +},{ + "id": "bender", + "template": "bender.jpg", + "aliases": ["hookers", "blackjack"], + "based_on": "2_texts" +},{ + "id": "alive", + "template": "alive.jpg", + "aliases": ["no_brain"], + "font": "grafo", + "font_size": 0.07, + "texts": [{ + "x_range": [0.05, 0.45], + "y_range": [0.53, 0.70], + "fill": [0, 14, 109], + "stroke_fill": [0, 14, 109], + "stroke_width": 0.02 + }] +},{ + "id": "argument", + "template": "argument.jpg", + "aliases": ["wrestlers"], + "font": "impact", + "font_size": 0.07, + "texts": [{ + "x_range": [0.02, 0.98], + "y_range": [0.10, 0.19], + "stroke_fill": [255, 255, 255], + "stroke_width": 0.1, + "position": "S" + },{ + "x_range": [0.02, 0.98], + "y_range": [0.30, 0.39], + "stroke_fill": [255, 255, 255], + "stroke_width": 0.1, + "position": "S" + },{ + "x_range": [0.02, 0.98], + "y_range": [0.50, 0.59], + "stroke_fill": [255, 255, 255], + "stroke_width": 0.1, + "position": "S" + },{ + "x_range": [0.02, 0.98], + "y_range": [0.70, 0.79], + "stroke_fill": [255, 255, 255], + "stroke_width": 0.1, + "position": "S" + },{ + "x_range": [0.02, 0.98], + "y_range": [0.90, 0.99], + "stroke_fill": [255, 255, 255], + "stroke_width": 0.1, + "position": "S" + }] +},{ + "id": "buff", + "template": "buff.jpg", + "aliases": ["tom", "jerry"], + "font": "impact", + "font_size": 0.07, + "texts": [] }] \ No newline at end of file diff --git a/templates/burn.jpg b/templates/burn.jpg index 6b6a40f..5fb133f 100644 Binary files a/templates/burn.jpg and b/templates/burn.jpg differ