Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -91,9 +91,9 @@ def process_image(image, text):
|
|
| 91 |
angle = detect_paper_angle(np.array(image), (x1, y1, x2, y2))
|
| 92 |
logging.debug(f"Detected paper angle: {angle} degrees.")
|
| 93 |
|
| 94 |
-
#
|
| 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
|
|
@@ -102,9 +102,9 @@ def process_image(image, text):
|
|
| 102 |
|
| 103 |
box_width = x2_padded - x1_padded
|
| 104 |
box_height = y2_padded - y1_padded
|
| 105 |
-
logging.debug(f"Padded
|
| 106 |
|
| 107 |
-
# Dynamically set font size based on
|
| 108 |
try:
|
| 109 |
font_size = min(int(box_height * 0.8), int(box_width / 10))
|
| 110 |
font = ImageFont.truetype(FONT_PATH, size=font_size)
|
|
@@ -118,7 +118,7 @@ def process_image(image, text):
|
|
| 118 |
text_draw = ImageDraw.Draw(text_layer)
|
| 119 |
logging.debug("Created transparent text layer.")
|
| 120 |
|
| 121 |
-
# Function to adjust font size until text fits within the
|
| 122 |
def adjust_font_size(lines, box_width, box_height, font, text_draw):
|
| 123 |
line_height = (text_draw.textbbox((0, 0), "Hg", font=font)[3] - text_draw.textbbox((0, 0), "Hg", font=font)[1]) * 1.2
|
| 124 |
total_text_height = len(lines) * line_height
|
|
@@ -131,7 +131,7 @@ def process_image(image, text):
|
|
| 131 |
total_text_height = len(lines) * line_height
|
| 132 |
return font, line_height
|
| 133 |
|
| 134 |
-
# Improved text wrapping logic to fit within the
|
| 135 |
words = text.split()
|
| 136 |
lines = []
|
| 137 |
current_line = ""
|
|
@@ -147,10 +147,10 @@ def process_image(image, text):
|
|
| 147 |
if current_line:
|
| 148 |
lines.append(current_line)
|
| 149 |
|
| 150 |
-
# Adjust font size if text height exceeds
|
| 151 |
font, line_height = adjust_font_size(lines, box_width, box_height, font, text_draw)
|
| 152 |
|
| 153 |
-
# Center the text block within the
|
| 154 |
start_y = y1_padded + (box_height - len(lines) * line_height) // 2
|
| 155 |
for i, line in enumerate(lines):
|
| 156 |
text_width = text_draw.textbbox((0, 0), line, font=font)[2] - text_draw.textbbox((0, 0), line, font=font)[0]
|
|
@@ -158,7 +158,7 @@ def process_image(image, text):
|
|
| 158 |
text_y = start_y + i * line_height
|
| 159 |
text_draw.text((text_x, text_y), line, fill=(0, 0, 0, 180), font=font)
|
| 160 |
|
| 161 |
-
logging.debug(f"Drew text within
|
| 162 |
|
| 163 |
# Apply rotation to align with the paper's angle
|
| 164 |
rotated_text_layer = text_layer.rotate(-angle, resample=Image.BICUBIC, center=(x1 + box_width / 2, y1 + box_height / 2))
|
|
@@ -220,3 +220,4 @@ if __name__ == "__main__":
|
|
| 220 |
|
| 221 |
|
| 222 |
|
|
|
|
|
|
| 91 |
angle = detect_paper_angle(np.array(image), (x1, y1, x2, y2))
|
| 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
|
|
|
|
| 102 |
|
| 103 |
box_width = x2_padded - x1_padded
|
| 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.8), int(box_width / 10))
|
| 110 |
font = ImageFont.truetype(FONT_PATH, size=font_size)
|
|
|
|
| 118 |
text_draw = ImageDraw.Draw(text_layer)
|
| 119 |
logging.debug("Created transparent text layer.")
|
| 120 |
|
| 121 |
+
# Function to adjust font size until text fits within the white paper
|
| 122 |
def adjust_font_size(lines, box_width, box_height, font, text_draw):
|
| 123 |
line_height = (text_draw.textbbox((0, 0), "Hg", font=font)[3] - text_draw.textbbox((0, 0), "Hg", font=font)[1]) * 1.2
|
| 124 |
total_text_height = len(lines) * line_height
|
|
|
|
| 131 |
total_text_height = len(lines) * line_height
|
| 132 |
return font, line_height
|
| 133 |
|
| 134 |
+
# Improved text wrapping logic to fit within the white paper
|
| 135 |
words = text.split()
|
| 136 |
lines = []
|
| 137 |
current_line = ""
|
|
|
|
| 147 |
if current_line:
|
| 148 |
lines.append(current_line)
|
| 149 |
|
| 150 |
+
# Adjust font size if text height exceeds the white paper height
|
| 151 |
font, line_height = adjust_font_size(lines, box_width, box_height, font, text_draw)
|
| 152 |
|
| 153 |
+
# Center the text block within the white paper
|
| 154 |
start_y = y1_padded + (box_height - len(lines) * line_height) // 2
|
| 155 |
for i, line in enumerate(lines):
|
| 156 |
text_width = text_draw.textbbox((0, 0), line, font=font)[2] - text_draw.textbbox((0, 0), line, font=font)[0]
|
|
|
|
| 158 |
text_y = start_y + i * line_height
|
| 159 |
text_draw.text((text_x, text_y), line, fill=(0, 0, 0, 180), font=font)
|
| 160 |
|
| 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(-angle, resample=Image.BICUBIC, center=(x1 + box_width / 2, y1 + box_height / 2))
|
|
|
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
+
|