Spaces:
Running
Running
File size: 1,116 Bytes
1211eb4 af0e34f 1211eb4 af0e34f |
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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
|