Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -92,8 +92,8 @@ def process_image(image, text):
|
|
| 92 |
logging.debug(f"Detected paper angle: {angle} degrees.")
|
| 93 |
|
| 94 |
# Adjust padding based on the white paper's detected dimensions
|
| 95 |
-
padding_x = int((x2 - x1) * 0.
|
| 96 |
-
padding_y = int((y2 - y1) * 0.
|
| 97 |
|
| 98 |
x1_padded = x1 + padding_x
|
| 99 |
y1_padded = y1 + padding_y
|
|
@@ -118,20 +118,26 @@ def process_image(image, text):
|
|
| 118 |
text_draw = ImageDraw.Draw(text_layer)
|
| 119 |
logging.debug("Created transparent text layer.")
|
| 120 |
|
| 121 |
-
#
|
| 122 |
def adjust_font_size(lines, box_width, box_height, font, text_draw):
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
if font_size < 10: # Set a minimum font size
|
| 128 |
-
break
|
| 129 |
-
font = ImageFont.truetype(FONT_PATH, size=font_size)
|
| 130 |
-
line_height = (text_draw.textbbox((0, 0), "Hg", font=font)[3] - text_draw.textbbox((0, 0), "Hg", font=font)[1]) * 1.2
|
| 131 |
total_text_height = len(lines) * line_height
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
return font, line_height
|
| 133 |
|
| 134 |
-
#
|
| 135 |
words = text.split()
|
| 136 |
lines = []
|
| 137 |
current_line = ""
|
|
@@ -161,7 +167,9 @@ def process_image(image, text):
|
|
| 161 |
logging.debug(f"Drew text within white paper: ({x1_padded}, {y1_padded}), ({x2_padded}, {y2_padded}).")
|
| 162 |
|
| 163 |
# Apply rotation to align with the paper's angle
|
| 164 |
-
rotated_text_layer = text_layer.rotate(
|
|
|
|
|
|
|
| 165 |
|
| 166 |
# Composite the rotated text layer onto the original image
|
| 167 |
pil_image = Image.alpha_composite(pil_image, rotated_text_layer)
|
|
@@ -221,3 +229,4 @@ if __name__ == "__main__":
|
|
| 221 |
|
| 222 |
|
| 223 |
|
|
|
|
|
|
| 92 |
logging.debug(f"Detected paper angle: {angle} degrees.")
|
| 93 |
|
| 94 |
# Adjust padding based on the white paper's detected dimensions
|
| 95 |
+
padding_x = int((x2 - x1) * 0.05) # 5% padding horizontally
|
| 96 |
+
padding_y = int((y2 - y1) * 0.05) # 5% padding vertically
|
| 97 |
|
| 98 |
x1_padded = x1 + padding_x
|
| 99 |
y1_padded = y1 + padding_y
|
|
|
|
| 118 |
text_draw = ImageDraw.Draw(text_layer)
|
| 119 |
logging.debug("Created transparent text layer.")
|
| 120 |
|
| 121 |
+
# Adjust font size to fit within the white paper
|
| 122 |
def adjust_font_size(lines, box_width, box_height, font, text_draw):
|
| 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 |
+
max_line_width = max(
|
| 129 |
+
text_draw.textbbox((0, 0), line, font=font)[2] - text_draw.textbbox((0, 0), line, font=font)[0]
|
| 130 |
+
for line in lines
|
| 131 |
+
)
|
| 132 |
+
if total_text_height <= box_height and max_line_width <= box_width:
|
| 133 |
+
return font, line_height
|
| 134 |
+
font_size = font.size - 2
|
| 135 |
+
if font_size < 10:
|
| 136 |
+
break # Minimum font size
|
| 137 |
+
font = ImageFont.truetype(FONT_PATH, size=font_size)
|
| 138 |
return font, line_height
|
| 139 |
|
| 140 |
+
# Split and wrap text to fit within the white paper
|
| 141 |
words = text.split()
|
| 142 |
lines = []
|
| 143 |
current_line = ""
|
|
|
|
| 167 |
logging.debug(f"Drew text within white paper: ({x1_padded}, {y1_padded}), ({x2_padded}, {y2_padded}).")
|
| 168 |
|
| 169 |
# Apply rotation to align with the paper's angle
|
| 170 |
+
rotated_text_layer = text_layer.rotate(
|
| 171 |
+
-angle, resample=Image.BICUBIC, center=(x1 + box_width / 2, y1 + box_height / 2)
|
| 172 |
+
)
|
| 173 |
|
| 174 |
# Composite the rotated text layer onto the original image
|
| 175 |
pil_image = Image.alpha_composite(pil_image, rotated_text_layer)
|
|
|
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
+
|