tiahchia's picture
Update First_agent_template/generate.py
1211eb4 verified
from PIL import Image, ImageDraw, ImageFont
from pathlib import Path
import textwrap
def generate_image_from_text(text: str) -> Image.Image:
"""
Generates a simple placeholder image for a lesson text.
Returns a PIL.Image object.
"""
# Image settings
width, height = 1024, 1024
background_color = (18, 18, 18) # dark background
text_color = (250, 41, 188) # pink-ish text
font_size = 24
# Create a blank image
img = Image.new("RGB", (width, height), color=background_color)
draw = ImageDraw.Draw(img)
# Load a default font
try:
font = ImageFont.truetype("Consolas.ttf", font_size)
except:
font = ImageFont.load_default()
# Wrap text to fit image width
margin = 40
max_width = width - 2 * margin
lines = textwrap.wrap(text, width=60) # Adjust width as needed
y_text = margin
for line in lines:
draw.text((margin, y_text), line, font=font, fill=text_color)
y_text += font_size + 8
if y_text > height - margin:
break # stop if text exceeds image height
return img