File size: 7,729 Bytes
4eeb4da
4932818
9b64245
8f38a6d
 
9b64245
 
8f38a6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b64245
8f38a6d
 
f4892cf
9b64245
f3608f2
f4892cf
f3608f2
fcc9054
9b64245
f4892cf
8f38a6d
f3608f2
9b64245
f4892cf
f3608f2
f4892cf
9b64245
f3608f2
9b64245
 
 
 
 
 
 
 
 
 
12f8f41
9b64245
 
 
 
 
 
f3608f2
 
 
 
 
 
 
 
 
 
 
 
9b64245
 
 
 
 
12f8f41
9b64245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12f8f41
9b64245
 
8f38a6d
9b64245
 
12f8f41
9b64245
 
 
12f8f41
9b64245
 
 
 
 
 
 
 
6ffa77a
9b64245
 
fcc9054
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b64245
 
 
 
 
 
 
 
 
 
fcc9054
9b64245
 
 
583daf5
9b64245
 
fcc9054
 
9b64245
fcc9054
 
 
 
 
f3608f2
9b64245
 
 
 
 
 
 
 
 
 
 
 
 
f3608f2
9b64245
f3608f2
9b64245
 
 
 
f4892cf
9b64245
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import os
import tempfile
import gradio as gr
from PIL import Image, ImageDraw, ImageFont

# --- Image Generation Logic (from coverter.py) ---

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."""
    # Draw vertical lines
    for x in range(0, width, spacing):
        draw.line([(x, 0), (x, height)], fill=color, width=2)
    # Draw horizontal lines
    for y in range(0, height, spacing):
        draw.line([(0, y), (width, y)], fill=color, width=2)


def text_to_images_generator(text_content, style='lines'):
    """
    Converts a given string of text into a single combined image and returns the file path.
    This is compatible with both UI and MCP.

    Args:
        text_content (str): The text to be converted.
        style (str): The background style ('plain', 'lines', 'dots', 'grid').

    Returns:
        str: Message with the path to the generated combined image file.
    """
    if not text_content or not text_content.strip():
        return "Error: Input text is empty. Please enter some text to generate images."

    # --- Configuration ---
    IMG_WIDTH = 1080
    IMG_HEIGHT = 1080
    BACKGROUND_COLOR = (255, 255, 255)
    TEXT_COLOR = (10, 10, 10)
    STYLE_COLOR = (225, 225, 225) # Color for lines/dots/grid
    
    PADDING_X = 80
    PADDING_Y = 80

    FONT_SIZE = 48
    LINE_SPACING = 20
    
    # --- Font Loading ---
    font = None
    try:
        font_paths_to_try = [
            "Arial.ttf", "arial.ttf", "DejaVuSans.ttf",
            "/System/Library/Fonts/Supplemental/Arial.ttf",
            "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
            "/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
        if not font:
            font = ImageFont.load_default()
    except Exception as e:
        print(f"An unexpected error occurred during font loading: {e}")
        font = ImageFont.load_default()

    # --- Text Wrapping Logic ---
    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 ---
    try:
        line_height = font.getbbox("A")[3] - font.getbbox("A")[1]
    except AttributeError:
        line_height = 12
    
    PARAGRAPH_SPACING = line_height

    # Calculate pages and total height needed
    pages_content = []
    current_page = []
    y_text = PADDING_Y

    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:
            pages_content.append(current_page)
            current_page = [item]
            y_text = PADDING_Y + item_height + (0 if is_break else LINE_SPACING)
        else:
            current_page.append(item)
            y_text += item_height + (0 if is_break else LINE_SPACING)
            
    if current_page:
        pages_content.append(current_page)

    # Create a single combined image with all pages
    total_height = len(pages_content) * IMG_HEIGHT
    combined_img = Image.new('RGB', (IMG_WIDTH, total_height), color=BACKGROUND_COLOR)
    
    for page_idx, page_content in enumerate(pages_content):
        # Create individual page
        page_img = Image.new('RGB', (IMG_WIDTH, IMG_HEIGHT), color=BACKGROUND_COLOR)
        draw = ImageDraw.Draw(page_img)

        if style == 'lines':
            line_style_spacing = line_height + LINE_SPACING
            draw_horizontal_lines(draw, IMG_WIDTH, IMG_HEIGHT, spacing=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 page_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
        
        # Paste this page onto the combined image
        combined_img.paste(page_img, (0, page_idx * IMG_HEIGHT))

    # Save combined image to temporary file
    temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
    combined_img.save(temp_file.name, format='PNG')
    temp_file.close()
    
    return f"Image successfully generated and saved to: {temp_file.name}\n\nImage details:\n- Total pages: {len(pages_content)}\n- Image dimensions: {IMG_WIDTH} x {total_height} pixels\n- Style: {style}\n- Format: PNG"

# --- 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."""

demo = gr.Interface(
    fn=text_to_images_generator,
    inputs=[
        gr.Textbox(lines=15, label="Text Content", placeholder="Paste your long-form text here...", value=example_text),
        gr.Radio(['lines', 'dots', 'grid', 'plain'], label="Background Style", value='lines')
    ],
    outputs=gr.Textbox(label="Result", show_label=True),
    title="Text-to-Image Converter",
    description="Transforms long-form text into a single combined image with multiple pages. Paste your text, choose a style, and click 'Submit'. The result will show the file path where your image was saved.",
    allow_flagging="never"
)

# --- Main Execution ---
if __name__ == "__main__":
    demo.launch(mcp_server=True)