Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -33,7 +33,9 @@ def add_text_to_image(image_path, text):
|
|
33 |
img_width, img_height = img.size
|
34 |
max_text_width = int(img_width * 0.9) # 90% of image width
|
35 |
lines = textwrap.wrap(text, width=40)
|
36 |
-
|
|
|
|
|
37 |
text_height = line_height * len(lines)
|
38 |
|
39 |
# Create semi-transparent background
|
@@ -50,7 +52,9 @@ def add_text_to_image(image_path, text):
|
|
50 |
# Add text
|
51 |
y_text = img_height - text_height - 10
|
52 |
for line in lines:
|
53 |
-
|
|
|
|
|
54 |
x_text = (img_width - line_width) / 2
|
55 |
draw.text((x_text, y_text), line, font=font, fill=(255, 255, 255, 255))
|
56 |
y_text += line_height
|
@@ -63,17 +67,6 @@ def add_text_to_image(image_path, text):
|
|
63 |
print(f"Error adding text to image: {e}")
|
64 |
return None
|
65 |
|
66 |
-
def visualize_story_lines(story, progress=gr.Progress()):
|
67 |
-
lines = [line.strip() for line in story.split('\n') if line.strip()]
|
68 |
-
images_with_text = []
|
69 |
-
|
70 |
-
def process_line(idx, line):
|
71 |
-
prompt = line.replace(" ", "_")
|
72 |
-
img_file = download_image(prompt, idx)
|
73 |
-
if img_file:
|
74 |
-
return add_text_to_image(img_file, line)
|
75 |
-
return None
|
76 |
-
|
77 |
with ThreadPoolExecutor(max_workers=5) as executor:
|
78 |
future_to_idx = {executor.submit(process_line, idx, line): idx for idx, line in enumerate(lines)}
|
79 |
for future in progress.tqdm(as_completed(future_to_idx), total=len(lines), desc="Processing images"):
|
|
|
33 |
img_width, img_height = img.size
|
34 |
max_text_width = int(img_width * 0.9) # 90% of image width
|
35 |
lines = textwrap.wrap(text, width=40)
|
36 |
+
|
37 |
+
# Use getbbox instead of getsize
|
38 |
+
line_height = font.getbbox("hg")[3] + 5 # Add some padding
|
39 |
text_height = line_height * len(lines)
|
40 |
|
41 |
# Create semi-transparent background
|
|
|
52 |
# Add text
|
53 |
y_text = img_height - text_height - 10
|
54 |
for line in lines:
|
55 |
+
# Use getbbox for line width calculation
|
56 |
+
bbox = font.getbbox(line)
|
57 |
+
line_width = bbox[2] - bbox[0]
|
58 |
x_text = (img_width - line_width) / 2
|
59 |
draw.text((x_text, y_text), line, font=font, fill=(255, 255, 255, 255))
|
60 |
y_text += line_height
|
|
|
67 |
print(f"Error adding text to image: {e}")
|
68 |
return None
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
with ThreadPoolExecutor(max_workers=5) as executor:
|
71 |
future_to_idx = {executor.submit(process_line, idx, line): idx for idx, line in enumerate(lines)}
|
72 |
for future in progress.tqdm(as_completed(future_to_idx), total=len(lines), desc="Processing images"):
|