Create utils.py
Browse files
utils.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image, ImageDraw, ImageFont
|
2 |
+
|
3 |
+
def generate_thumbnail(input_image, text, font_path=None, font_size=60, position="bottom", text_color="white"):
|
4 |
+
image = input_image.convert("RGB")
|
5 |
+
draw = ImageDraw.Draw(image)
|
6 |
+
|
7 |
+
# Load font
|
8 |
+
if font_path:
|
9 |
+
font = ImageFont.truetype(font_path, font_size)
|
10 |
+
else:
|
11 |
+
font = ImageFont.load_default()
|
12 |
+
|
13 |
+
text_width, text_height = draw.textsize(text, font=font)
|
14 |
+
image_width, image_height = image.size
|
15 |
+
|
16 |
+
if position == "bottom":
|
17 |
+
x = (image_width - text_width) // 2
|
18 |
+
y = image_height - text_height - 20
|
19 |
+
elif position == "top":
|
20 |
+
x = (image_width - text_width) // 2
|
21 |
+
y = 20
|
22 |
+
else: # center
|
23 |
+
x = (image_width - text_width) // 2
|
24 |
+
y = (image_height - text_height) // 2
|
25 |
+
|
26 |
+
draw.text((x, y), text, fill=text_color, font=font)
|
27 |
+
return image
|