Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from PIL import Image, ImageDraw, ImageFont | |
| import textwrap | |
| import io | |
| import os | |
| def text_to_image(text_content, font_size_option): | |
| """ | |
| Convert text content to a formatted image with customizable font size. | |
| Args: | |
| text_content (str): The input text to convert to image | |
| font_size_option (str): Font size option (Small, Medium, Large) | |
| Returns: | |
| PIL.Image: Generated image | |
| """ | |
| if not text_content.strip(): | |
| text_content = "Please enter some text to convert to image." | |
| # Font size mapping | |
| font_sizes = { | |
| "Small": 20, | |
| "Medium": 28, | |
| "Large": 36 | |
| } | |
| font_size = font_sizes.get(font_size_option, 28) | |
| # Fixed styling parameters | |
| img_width = 600 # 3:4 ratio width | |
| img_height = 800 # 3:4 ratio height | |
| background_color = "#F8F9FA" # Light theme background | |
| text_color = "#000000" # Black text | |
| padding = 50 | |
| line_spacing = font_size // 3 # Dynamic line spacing based on font size | |
| # Create image with higher quality | |
| img = Image.new('RGB', (img_width, img_height), background_color) | |
| draw = ImageDraw.Draw(img) | |
| # Try to use a nice font with better quality, fallback to default if not available | |
| font = None | |
| font_paths = [ | |
| "/System/Library/Fonts/Helvetica.ttc", # macOS | |
| "/System/Library/Fonts/Arial.ttf", # macOS | |
| "/Windows/Fonts/arial.ttf", # Windows | |
| "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf", # Linux | |
| "arial.ttf" | |
| ] | |
| for font_path in font_paths: | |
| try: | |
| font = ImageFont.truetype(font_path, font_size) | |
| break | |
| except: | |
| continue | |
| if font is None: | |
| try: | |
| font = ImageFont.load_default() | |
| except: | |
| # Fallback font | |
| font = ImageFont.load_default() | |
| # Calculate text area with better precision | |
| text_width = img_width - (padding * 2) | |
| # More accurate text wrapping based on actual character width | |
| # Test character width | |
| test_char_width = draw.textbbox((0, 0), "W", font=font)[2] | |
| chars_per_line = max(1, text_width // test_char_width) | |
| wrapped_lines = [] | |
| paragraphs = text_content.split('\n') | |
| for paragraph in paragraphs: | |
| if paragraph.strip(): | |
| lines = textwrap.wrap(paragraph, width=chars_per_line) | |
| wrapped_lines.extend(lines) | |
| wrapped_lines.append("") # Add space between paragraphs | |
| else: | |
| wrapped_lines.append("") | |
| # Remove trailing empty lines | |
| while wrapped_lines and wrapped_lines[-1] == "": | |
| wrapped_lines.pop() | |
| # Draw text with better positioning | |
| y_position = padding | |
| for line in wrapped_lines: | |
| if y_position + font_size + line_spacing > img_height - padding: | |
| # Add "..." if text is too long | |
| draw.text((padding, y_position), "...", font=font, fill=text_color) | |
| break | |
| # Use textbbox for better text positioning | |
| draw.text((padding, y_position), line, font=font, fill=text_color) | |
| y_position += font_size + line_spacing | |
| return img | |
| demo = gr.Interface( | |
| fn=text_to_image, | |
| inputs=[ | |
| gr.Textbox( | |
| label="Text Content", | |
| placeholder="Enter your text content here...", | |
| lines=10, | |
| value="Welcome to Text-to-Image Converter!\n\nThis tool converts your text into beautifully formatted images perfect for social media posts.\n\nSimply replace this text with your content, choose your preferred font size, and click generate!" | |
| ), | |
| gr.Radio( | |
| choices=["Small", "Medium", "Large"], | |
| value="Medium", | |
| label="Font Size" | |
| ) | |
| ], | |
| outputs=[gr.Image(label="Generated Image", type="pil")], | |
| title="Text to Image Converter", | |
| description="Convert your text content into attractive images with a clean, readable design. Choose your font size and generate high-quality images perfect for social media!" | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(mcp_server=True) |