File size: 1,418 Bytes
dfb17f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()