Update Meme Generator project
This commit is contained in:
@@ -11,24 +11,33 @@ class MemeEngine:
|
||||
"""Initialize meme engine with path to save generated memes."""
|
||||
self.output_path = output_path
|
||||
|
||||
def resize_image(self, img, width=500):
|
||||
"""Resize image to specified width while maintaining aspect ratio."""
|
||||
if img.size[0] > width:
|
||||
ratio = width / (img.size[0] * 1.0)
|
||||
height = ratio * img.size[1]
|
||||
img = img.resize((int(width), int(height)), Image.NEAREST)
|
||||
return img
|
||||
|
||||
def draw_text(self, image, message):
|
||||
"""Draw text on the image."""
|
||||
draw = ImageDraw.Draw(image)
|
||||
font = ImageFont.truetype(
|
||||
"./_data/font/calibri_regular.ttf", int(image.size[1] / 20)
|
||||
)
|
||||
draw.text((20, 20), message, font=font, fill="white")
|
||||
return image
|
||||
|
||||
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"
|
||||
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)
|
||||
image_resize = self.resize_image(img, width=width)
|
||||
image_draw = self.draw_text(image_resize, message)
|
||||
image_draw.save(output_file)
|
||||
except Exception as err:
|
||||
print(f"Error: {err}")
|
||||
|
||||
|
||||
@@ -1 +1,6 @@
|
||||
"""MemeEngine package.
|
||||
|
||||
Provides the MemeEngine class for generating meme images from templates.
|
||||
"""
|
||||
|
||||
from .MemeEngine import MemeEngine
|
||||
|
||||
Reference in New Issue
Block a user