Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -104,57 +104,55 @@ def process_image(image, text):
|
|
| 104 |
box_height = y2_padded - y1_padded
|
| 105 |
logging.debug(f"Padded white paper dimensions: width={box_width}, height={box_height}.")
|
| 106 |
|
| 107 |
-
# Dynamically set font size based on the white paper dimensions
|
| 108 |
-
try:
|
| 109 |
-
font_size = min(int(box_height * 0.7), int(box_width / 10))
|
| 110 |
-
font = ImageFont.truetype(FONT_PATH, size=font_size)
|
| 111 |
-
logging.debug("Font loaded successfully.")
|
| 112 |
-
except Exception as e:
|
| 113 |
-
logging.warning(f"Error loading font. Using default. {e}")
|
| 114 |
-
font = ImageFont.load_default()
|
| 115 |
-
|
| 116 |
# Create a transparent text layer
|
| 117 |
text_layer = Image.new("RGBA", pil_image.size, (255, 255, 255, 0))
|
| 118 |
text_draw = ImageDraw.Draw(text_layer)
|
| 119 |
logging.debug("Created transparent text layer.")
|
| 120 |
|
| 121 |
-
#
|
| 122 |
-
def
|
| 123 |
while True:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
line_height = (
|
| 125 |
text_draw.textbbox((0, 0), "Hg", font=font)[3] - text_draw.textbbox((0, 0), "Hg", font=font)[1]
|
| 126 |
) * 1.2
|
| 127 |
total_text_height = len(lines) * line_height
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
return font, line_height
|
| 134 |
font_size = font.size - 2
|
| 135 |
-
if font_size < 10:
|
| 136 |
-
|
|
|
|
| 137 |
font = ImageFont.truetype(FONT_PATH, size=font_size)
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
current_line = test_line
|
| 150 |
-
else:
|
| 151 |
-
lines.append(current_line)
|
| 152 |
-
current_line = word
|
| 153 |
-
if current_line:
|
| 154 |
-
lines.append(current_line)
|
| 155 |
-
|
| 156 |
-
# Adjust font size if text height exceeds the white paper height
|
| 157 |
-
font, line_height = adjust_font_size(lines, box_width, box_height, font, text_draw)
|
| 158 |
|
| 159 |
# Center the text block within the white paper
|
| 160 |
start_y = y1_padded + (box_height - len(lines) * line_height) // 2
|
|
@@ -230,3 +228,4 @@ if __name__ == "__main__":
|
|
| 230 |
|
| 231 |
|
| 232 |
|
|
|
|
|
|
| 104 |
box_height = y2_padded - y1_padded
|
| 105 |
logging.debug(f"Padded white paper dimensions: width={box_width}, height={box_height}.")
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
# Create a transparent text layer
|
| 108 |
text_layer = Image.new("RGBA", pil_image.size, (255, 255, 255, 0))
|
| 109 |
text_draw = ImageDraw.Draw(text_layer)
|
| 110 |
logging.debug("Created transparent text layer.")
|
| 111 |
|
| 112 |
+
# Function to validate and reduce font size if necessary
|
| 113 |
+
def draw_text_with_fit(text, box_width, box_height, font, text_draw):
|
| 114 |
while True:
|
| 115 |
+
words = text.split()
|
| 116 |
+
lines = []
|
| 117 |
+
current_line = ""
|
| 118 |
+
|
| 119 |
+
for word in words:
|
| 120 |
+
test_line = f"{current_line} {word}".strip()
|
| 121 |
+
bbox = text_draw.textbbox((0, 0), test_line, font=font)
|
| 122 |
+
if bbox[2] - bbox[0] <= box_width * 0.9:
|
| 123 |
+
current_line = test_line
|
| 124 |
+
else:
|
| 125 |
+
lines.append(current_line)
|
| 126 |
+
current_line = word
|
| 127 |
+
if current_line:
|
| 128 |
+
lines.append(current_line)
|
| 129 |
+
|
| 130 |
+
# Calculate total text height
|
| 131 |
line_height = (
|
| 132 |
text_draw.textbbox((0, 0), "Hg", font=font)[3] - text_draw.textbbox((0, 0), "Hg", font=font)[1]
|
| 133 |
) * 1.2
|
| 134 |
total_text_height = len(lines) * line_height
|
| 135 |
+
|
| 136 |
+
# Check if text fits within the box
|
| 137 |
+
if total_text_height <= box_height:
|
| 138 |
+
return lines, font, line_height
|
| 139 |
+
# Reduce font size
|
|
|
|
| 140 |
font_size = font.size - 2
|
| 141 |
+
if font_size < 10: # Minimum font size
|
| 142 |
+
logging.warning("Minimum font size reached; text may still exceed bounds.")
|
| 143 |
+
return lines, font, line_height
|
| 144 |
font = ImageFont.truetype(FONT_PATH, size=font_size)
|
| 145 |
+
|
| 146 |
+
# Dynamically set font size and validate
|
| 147 |
+
try:
|
| 148 |
+
initial_font_size = min(int(box_height * 0.7), int(box_width / 10))
|
| 149 |
+
font = ImageFont.truetype(FONT_PATH, size=initial_font_size)
|
| 150 |
+
logging.debug("Font loaded successfully.")
|
| 151 |
+
except Exception as e:
|
| 152 |
+
logging.warning(f"Error loading font. Using default. {e}")
|
| 153 |
+
font = ImageFont.load_default()
|
| 154 |
+
|
| 155 |
+
lines, font, line_height = draw_text_with_fit(text, box_width, box_height, font, text_draw)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
# Center the text block within the white paper
|
| 158 |
start_y = y1_padded + (box_height - len(lines) * line_height) // 2
|
|
|
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
+
|