"""Meme engine module for handling the generation of meme images.""" import random from PIL import Image, ImageFont, ImageDraw class MemeEngine: """Class to generate memes from images and text.""" def __init__(self, output_path): """Initialize meme engine with path to save generated memes.""" self.output_path = output_path def make_meme(self, img_path, text, author, width=500) -> str: """Generate a meme image with given text and author.""" output_file = f"{self.output_path}/{random.randint(0,10000)}.jpg" message = f"{text}\n- {author}" try: with Image.open(img_path) as img: # Resize image while maintaining aspect ratio width = 500 if img.size[0] > 500 else img.size[0] ratio = width / (img.size[0] * 1.0) height = ratio * img.size[1] img = img.resize((int(width), int(height)), Image.NEAREST) draw = ImageDraw.Draw(img) font = ImageFont.truetype( "./_data/font/calibri_regular.ttf", int(height / 20) ) draw.text((20, 20), message, font=font, fill="white") img.save(output_file) except Exception as err: print(f"Error: {err}") return output_file