Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
from PIL import Image, ImageDraw, ImageFont | |
import base64 | |
import io | |
# --- Image Generation Logic --- | |
def draw_horizontal_lines(draw, width, height, spacing=60, color=(230, 230, 230)): | |
"""Draws horizontal lines on the image.""" | |
for y in range(0, height, spacing): | |
draw.line([(0, y), (width, y)], fill=color, width=2) | |
def draw_dot_grid(draw, width, height, spacing=50, color=(220, 220, 220)): | |
"""Draws a dot grid on the image.""" | |
for x in range(0, width, spacing): | |
for y in range(0, height, spacing): | |
draw.ellipse([(x-2, y-2), (x+2, y+2)], fill=color) | |
def draw_lattice_grid(draw, width, height, spacing=100, color=(235, 235, 235)): | |
"""Draws a lattice/graph paper grid on the image.""" | |
for x in range(0, width, spacing): | |
draw.line([(x, 0), (x, height)], fill=color, width=2) | |
for y in range(0, height, spacing): | |
draw.line([(0, y), (width, y)], fill=color, width=2) | |
def text_to_images_and_base64_generator(text_content, style, font_size, bg_color_name, aspect_ratio_str, font_choice): | |
""" | |
Converts text into images, returning both PIL objects for UI preview | |
and base64 strings for an API/MCP client, with customizable options. | |
""" | |
if not text_content or not text_content.strip(): | |
gr.Warning("Input text is empty. Please enter some text to generate images.") | |
return [], [] | |
# --- Mappings for UI options --- | |
aspect_ratio_map = { | |
"1:1 (Square)": (1080, 1080), | |
"16:9 (Widescreen)": (1080, 608), | |
"9:16 (Vertical)": (1080, 1920), | |
"4:3 (Standard)": (1080, 810), | |
"3:4 (Standard)": (1080, 1440) | |
} | |
color_map = { | |
"White": (255, 255, 255), | |
"Light Gray": (240, 240, 240), | |
"Beige": (245, 245, 220), | |
"Pale Blue": (220, 230, 245) | |
} | |
# **IMPORTANT**: This now points to specific files you need to upload. | |
# The key is the user-friendly name, the value is the file path. | |
font_map = { | |
"Arial": os.path.join("fonts", "LiberationSans-Regular.ttf"), | |
"Times New Roman": os.path.join("fonts", "LiberationSerif-Regular.ttf"), | |
"Courier New": os.path.join("fonts", "DejaVuSansMono.ttf"), | |
"DejaVu Sans": os.path.join("fonts", "DejaVuSans.ttf") | |
} | |
IMG_WIDTH, IMG_HEIGHT = aspect_ratio_map.get(aspect_ratio_str, (1080, 1080)) | |
BACKGROUND_COLOR = color_map.get(bg_color_name, (255, 255, 255)) | |
# --- Configuration --- | |
TEXT_COLOR = (10, 10, 10) | |
STYLE_COLOR = (225, 225, 225) | |
PADDING_X = 80 | |
PADDING_Y = 80 | |
LINE_SPACING = 20 | |
# --- Font Loading --- | |
font = None | |
font_path = font_map.get(font_choice, os.path.join("fonts", "DejaVuSans.ttf")) | |
try: | |
# Check if the fonts directory and the specific font file exist | |
if os.path.exists(font_path): | |
font = ImageFont.truetype(font_path, font_size) | |
else: | |
# Fallback if font is not found in the local directory | |
gr.Warning(f"Font file not found at '{font_path}'. Trying system fallbacks.") | |
font_paths_to_try = ["/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"] | |
for f_path in font_paths_to_try: | |
try: | |
font = ImageFont.truetype(f_path, font_size) | |
break | |
except IOError: | |
continue | |
except IOError: | |
pass # Will be handled by the final fallback | |
if not font: | |
font = ImageFont.load_default() | |
gr.Warning(f"Could not load '{font_choice}'. Using basic default font.") | |
# --- Text Wrapping --- | |
drawable_width = IMG_WIDTH - 2 * PADDING_X | |
paragraphs = [p.strip() for p in text_content.strip().split('\n') if p.strip()] | |
all_lines_and_breaks = [] | |
for i, paragraph in enumerate(paragraphs): | |
words = paragraph.split() | |
current_line = "" | |
for word in words: | |
if font.getlength(word) > drawable_width: | |
temp_word = "" | |
for char in word: | |
if font.getlength(temp_word + char) > drawable_width: | |
all_lines_and_breaks.append(temp_word) | |
temp_word = char | |
else: | |
temp_word += char | |
word = temp_word | |
if font.getlength(current_line + " " + word) <= drawable_width: | |
current_line += " " + word | |
else: | |
all_lines_and_breaks.append(current_line.strip()) | |
current_line = word | |
all_lines_and_breaks.append(current_line.strip()) | |
if i < len(paragraphs) - 1: | |
all_lines_and_breaks.append(None) | |
# --- Image Generation & Encoding --- | |
generated_images = [] | |
generated_base64_list = [] | |
page_content = [] | |
y_text = PADDING_Y | |
try: | |
line_height = font.getbbox("A")[3] - font.getbbox("A")[1] | |
except AttributeError: | |
bbox = font.getmask("A").getbbox() | |
line_height = bbox[3] - bbox[1] if bbox else 12 | |
PARAGRAPH_SPACING = line_height | |
def create_and_encode_page(content): | |
"""Helper to create a PIL image, draw text, and encode to base64.""" | |
img = Image.new('RGB', (IMG_WIDTH, IMG_HEIGHT), color=BACKGROUND_COLOR) | |
draw = ImageDraw.Draw(img) | |
if style == 'lines': | |
line_style_spacing = line_height + LINE_SPACING | |
draw_horizontal_lines(draw, IMG_WIDTH, IMG_HEIGHT, spacing=int(line_style_spacing), color=STYLE_COLOR) | |
elif style == 'dots': | |
draw_dot_grid(draw, IMG_WIDTH, IMG_HEIGHT, color=STYLE_COLOR) | |
elif style == 'grid': | |
draw_lattice_grid(draw, IMG_WIDTH, IMG_HEIGHT, color=STYLE_COLOR) | |
current_y = PADDING_Y | |
for page_item in content: | |
if page_item is not None: | |
draw.text((PADDING_X, current_y), page_item, font=font, fill=TEXT_COLOR) | |
current_y += line_height + LINE_SPACING | |
else: | |
current_y += PARAGRAPH_SPACING | |
generated_images.append(img) | |
buffered = io.BytesIO() | |
img.save(buffered, format="PNG") | |
img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8") | |
generated_base64_list.append(img_base64) | |
# --- Page Creation Loop --- | |
for item in all_lines_and_breaks: | |
is_break = item is None | |
item_height = PARAGRAPH_SPACING if is_break else line_height | |
if y_text + item_height > IMG_HEIGHT - PADDING_Y: | |
create_and_encode_page(page_content) | |
page_content = [item] | |
y_text = PADDING_Y + item_height + (0 if is_break else LINE_SPACING) | |
else: | |
page_content.append(item) | |
y_text += item_height + (0 if is_break else LINE_SPACING) | |
if page_content: | |
create_and_encode_page(page_content) | |
return generated_images, generated_base64_list | |
# --- Gradio Interface --- | |
example_text = """In the heart of a bustling city, there lived a clockmaker named Alistair. His shop, a quaint corner of tranquility amidst the urban chaos, was filled with the gentle ticking of countless timepieces. Each clock was a masterpiece, a testament to his dedication and skill. | |
One day, a young girl with eyes as curious as a cat's wandered into his shop. She wasn't interested in the shiny new watches but was drawn to the grandfather clock in the corner. "What's its story?" she asked, her voice soft. Alistair smiled, for he knew he had found the next guardian of the stories. The legacy of the whispering clock would live on.""" | |
# Get the list of font names for the dropdown | |
font_choices = ["Arial", "Times New Roman", "Courier New", "DejaVu Sans"] | |
demo = gr.Interface( | |
fn=text_to_images_and_base64_generator, | |
inputs=[ | |
gr.Textbox(lines=10, label="Text Content", placeholder="Paste your long-form text here...", value=example_text), | |
gr.Radio(['lines', 'dots', 'grid', 'plain'], label="Background Style", value='lines'), | |
gr.Slider(24, 72, value=48, step=2, label="Font Size"), | |
gr.Radio(["White", "Light Gray", "Beige", "Pale Blue"], label="Background Color", value="White"), | |
gr.Radio(["1:1 (Square)", "16:9 (Widescreen)", "9:16 (Vertical)", "4:3 (Standard)","3:4 (Standard)"], label="Aspect Ratio", value="1:1 (Square)"), | |
gr.Dropdown(font_choices, label="Font Family", value="Arial") | |
], | |
outputs=[ | |
gr.Gallery(label="Generated Images (Preview)", show_label=True, preview=True), | |
gr.JSON(label="API Output (Base64 Strings)") | |
], | |
title="Text-to-Image Server", | |
description="Transforms long-form text into images. A gallery is shown for UI preview, and a JSON list of base64 strings is returned for API/MCP clients.", | |
allow_flagging="never" | |
) | |
# --- Main Execution --- | |
if __name__ == "__main__": | |
# Before launching, check if the fonts directory exists. | |
if not os.path.exists("fonts"): | |
print("---") | |
print("WARNING: 'fonts' directory not found.") | |
print("Please create a 'fonts' directory and add .ttf font files to it.") | |
print("The application will try to use system fonts, but may fail.") | |
print("---") | |
demo.launch(mcp_server=True) | |