File size: 863 Bytes
14eaab5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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)

    # Load font
    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:  # center
        x = (image_width - text_width) // 2
        y = (image_height - text_height) // 2

    draw.text((x, y), text, fill=text_color, font=font)
    return image