Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from PIL import Image, ImageDraw, ImageFont | |
| import textwrap | |
| def create_thumbnail(title, bg_color, text_color): | |
| # Thumbnail size (YouTube recommended: 1280x720) | |
| img = Image.new("RGB", (1280, 720), color=bg_color) | |
| draw = ImageDraw.Draw(img) | |
| # Try loading a font | |
| try: | |
| font = ImageFont.truetype("arial.ttf", 80) | |
| except: | |
| font = ImageFont.load_default() | |
| # Wrap text to fit thumbnail | |
| wrapped_text = textwrap.fill(title, width=20) | |
| # Get text size | |
| text_width, text_height = draw.multiline_textsize(wrapped_text, font=font) | |
| # Position text in center | |
| x = (1280 - text_width) / 2 | |
| y = (720 - text_height) / 2 | |
| # Draw text | |
| draw.multiline_text((x, y), wrapped_text, fill=text_color, font=font, align="center") | |
| return img | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🖼 YouTube Thumbnail Generator") | |
| title_input = gr.Textbox(label="Video Title", placeholder="Enter your YouTube video title here") | |
| bg_color_input = gr.ColorPicker(label="Background Color", value="#FF0000") | |
| text_color_input = gr.ColorPicker(label="Text Color", value="#FFFFFF") | |
| generate_btn = gr.Button("Generate Thumbnail") | |
| output_img = gr.Image(label="Generated Thumbnail") | |
| generate_btn.click( | |
| create_thumbnail, | |
| inputs=[title_input, bg_color_input, text_color_input], | |
| outputs=output_img | |
| ) | |
| demo.launch() |