Stiphan commited on
Commit
dfb17f5
·
verified ·
1 Parent(s): 77da585

Add YouTube Thumbnail Generator app using Gradio and Pillow

Browse files

Includes gradio and pillow libraries for generating YouTube thumbnails on Hugging Face Spaces.

Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image, ImageDraw, ImageFont
3
+ import textwrap
4
+
5
+ def create_thumbnail(title, bg_color, text_color):
6
+ # Thumbnail size (YouTube recommended: 1280x720)
7
+ img = Image.new("RGB", (1280, 720), color=bg_color)
8
+
9
+ draw = ImageDraw.Draw(img)
10
+
11
+ # Try loading a font
12
+ try:
13
+ font = ImageFont.truetype("arial.ttf", 80)
14
+ except:
15
+ font = ImageFont.load_default()
16
+
17
+ # Wrap text to fit thumbnail
18
+ wrapped_text = textwrap.fill(title, width=20)
19
+
20
+ # Get text size
21
+ text_width, text_height = draw.multiline_textsize(wrapped_text, font=font)
22
+
23
+ # Position text in center
24
+ x = (1280 - text_width) / 2
25
+ y = (720 - text_height) / 2
26
+
27
+ # Draw text
28
+ draw.multiline_text((x, y), wrapped_text, fill=text_color, font=font, align="center")
29
+
30
+ return img
31
+
32
+ with gr.Blocks() as demo:
33
+ gr.Markdown("# 🖼 YouTube Thumbnail Generator")
34
+
35
+ title_input = gr.Textbox(label="Video Title", placeholder="Enter your YouTube video title here")
36
+ bg_color_input = gr.ColorPicker(label="Background Color", value="#FF0000")
37
+ text_color_input = gr.ColorPicker(label="Text Color", value="#FFFFFF")
38
+ generate_btn = gr.Button("Generate Thumbnail")
39
+ output_img = gr.Image(label="Generated Thumbnail")
40
+
41
+ generate_btn.click(
42
+ create_thumbnail,
43
+ inputs=[title_input, bg_color_input, text_color_input],
44
+ outputs=output_img
45
+ )
46
+
47
+ demo.launch()