Update Meme Generator project

This commit is contained in:
2026-01-05 09:18:45 -08:00
parent 433f2ba034
commit ecca39ca96

View File

@@ -25,7 +25,20 @@ class MemeEngine:
font = ImageFont.truetype(
"./_data/font/calibri_regular.ttf", int(image.size[1] / 20)
)
draw.text((20, 20), message, font=font, fill="white")
# Calculate text bounding box to ensure it fits on the image
text_bbox = draw.textbbox((0, 0), message, font=font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
# Generate random position that keeps text within image bounds
max_x = max(0, image.size[0] - text_width - 10)
max_y = max(0, image.size[1] - text_height - 10)
x = random.randint(10, max_x) if max_x > 10 else 10
y = random.randint(10, max_y) if max_y > 10 else 10
draw.text((x, y), message, font=font, fill="white")
return image
def make_meme(self, img_path, text, author, width=500) -> str: