SUN / utils.py
Bagda's picture
Create utils.py
14eaab5 verified
raw
history blame contribute delete
863 Bytes
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