Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -39,10 +39,10 @@ def get_uploaded_logo():
|
|
39 |
# Create image slides from text chunks
|
40 |
def create_slides(text, duration, output_folder, max_lines=6):
|
41 |
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
42 |
-
font = ImageFont.truetype(font_path,
|
43 |
logo_path = get_uploaded_logo()
|
44 |
|
45 |
-
chunks = textwrap.wrap(text, width=
|
46 |
slides = ["\n".join(chunks[i:i+max_lines]) for i in range(0, len(chunks), max_lines)]
|
47 |
per_slide_time = duration / len(slides)
|
48 |
slide_paths = []
|
@@ -52,18 +52,21 @@ def create_slides(text, duration, output_folder, max_lines=6):
|
|
52 |
draw = ImageDraw.Draw(img)
|
53 |
|
54 |
lines = slide_text.split("\n")
|
55 |
-
|
56 |
-
|
|
|
57 |
|
58 |
-
for line in lines:
|
59 |
-
bbox = draw.textbbox((0, 0), line, font=font)
|
60 |
w = bbox[2] - bbox[0]
|
61 |
h = bbox[3] - bbox[1]
|
62 |
draw.text(((1280 - w) // 2, y), line, font=font, fill="white")
|
63 |
-
y += h +
|
64 |
|
65 |
-
logo = Image.open(logo_path).convert("RGBA")
|
66 |
-
|
|
|
|
|
|
|
67 |
|
68 |
frame_path = os.path.join(output_folder, f"slide_{i}.png")
|
69 |
img.save(frame_path)
|
|
|
39 |
# Create image slides from text chunks
|
40 |
def create_slides(text, duration, output_folder, max_lines=6):
|
41 |
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
42 |
+
font = ImageFont.truetype(font_path, 48)
|
43 |
logo_path = get_uploaded_logo()
|
44 |
|
45 |
+
chunks = textwrap.wrap(text, width=36)
|
46 |
slides = ["\n".join(chunks[i:i+max_lines]) for i in range(0, len(chunks), max_lines)]
|
47 |
per_slide_time = duration / len(slides)
|
48 |
slide_paths = []
|
|
|
52 |
draw = ImageDraw.Draw(img)
|
53 |
|
54 |
lines = slide_text.split("\n")
|
55 |
+
line_sizes = [draw.textbbox((0, 0), line, font=font) for line in lines]
|
56 |
+
total_height = sum([b[3] - b[1] for b in line_sizes]) + (len(lines)-1)*20
|
57 |
+
y = max((720 - total_height) // 2, 20)
|
58 |
|
59 |
+
for line, bbox in zip(lines, line_sizes):
|
|
|
60 |
w = bbox[2] - bbox[0]
|
61 |
h = bbox[3] - bbox[1]
|
62 |
draw.text(((1280 - w) // 2, y), line, font=font, fill="white")
|
63 |
+
y += h + 20
|
64 |
|
65 |
+
logo = Image.open(logo_path).convert("RGBA")
|
66 |
+
logo_width = min(180, int(0.15 * img.width))
|
67 |
+
logo_height = int(logo.size[1] * (logo_width / logo.size[0]))
|
68 |
+
logo = logo.resize((logo_width, logo_height))
|
69 |
+
img.paste(logo, (img.width - logo_width - 30, img.height - logo_height - 30), logo)
|
70 |
|
71 |
frame_path = os.path.join(output_folder, f"slide_{i}.png")
|
72 |
img.save(frame_path)
|