|
|
from PIL import Image, ImageDraw, ImageFont |
|
|
|
|
|
def generate_thumbnail(input_image, text, font_path=None, font_size=60, position="bottom", text_color="white"): |
|
|
image = input_image.convert("RGB") |
|
|
draw = ImageDraw.Draw(image) |
|
|
|
|
|
|
|
|
if font_path: |
|
|
font = ImageFont.truetype(font_path, font_size) |
|
|
else: |
|
|
font = ImageFont.load_default() |
|
|
|
|
|
text_width, text_height = draw.textsize(text, font=font) |
|
|
image_width, image_height = image.size |
|
|
|
|
|
if position == "bottom": |
|
|
x = (image_width - text_width) // 2 |
|
|
y = image_height - text_height - 20 |
|
|
elif position == "top": |
|
|
x = (image_width - text_width) // 2 |
|
|
y = 20 |
|
|
else: |
|
|
x = (image_width - text_width) // 2 |
|
|
y = (image_height - text_height) // 2 |
|
|
|
|
|
draw.text((x, y), text, fill=text_color, font=font) |
|
|
return image |