3morrrrr commited on
Commit
cfd0c4a
·
verified ·
1 Parent(s): 407c28e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -15
app.py CHANGED
@@ -71,7 +71,7 @@ def process_image(image, text):
71
  pil_warped = Image.fromarray(cv2.cvtColor(warped, cv2.COLOR_BGRA2RGBA))
72
  draw = ImageDraw.Draw(pil_warped)
73
 
74
- # Calculate dynamic font size and write text
75
  font_size = max(10, int(height * 0.2)) # Adjust size relative to height
76
  try:
77
  font = ImageFont.truetype(FONT_PATH, size=font_size)
@@ -80,20 +80,29 @@ def process_image(image, text):
80
  logging.warning(f"Error loading font. Using default. {e}")
81
  font = ImageFont.load_default()
82
 
83
- bbox = draw.textbbox((0, 0), text, font=font)
84
- text_width = bbox[2] - bbox[0]
85
- text_height = bbox[3] - bbox[1]
86
-
87
- text_position = ((width - text_width) // 2, (height - text_height) // 2)
88
-
89
- # Debugging rectangle for text position
90
- draw.rectangle(
91
- [text_position, (text_position[0] + text_width, text_position[1] + text_height)],
92
- outline="red", # Debugging rectangle color
93
- width=2
94
- )
95
-
96
- draw.text(text_position, text, fill=(0, 0, 0, 255), font=font)
 
 
 
 
 
 
 
 
 
97
 
98
  # Convert back to OpenCV and reapply the perspective transformation
99
  warped_with_text = cv2.cvtColor(np.array(pil_warped), cv2.COLOR_RGBA2BGRA)
@@ -151,3 +160,4 @@ if __name__ == "__main__":
151
  interface.launch(share=True)
152
 
153
 
 
 
71
  pil_warped = Image.fromarray(cv2.cvtColor(warped, cv2.COLOR_BGRA2RGBA))
72
  draw = ImageDraw.Draw(pil_warped)
73
 
74
+ # Calculate dynamic font size and adjust for multi-line text
75
  font_size = max(10, int(height * 0.2)) # Adjust size relative to height
76
  try:
77
  font = ImageFont.truetype(FONT_PATH, size=font_size)
 
80
  logging.warning(f"Error loading font. Using default. {e}")
81
  font = ImageFont.load_default()
82
 
83
+ # Split text into multiple lines to fit within the bounding box
84
+ words = text.split()
85
+ lines = []
86
+ current_line = ""
87
+ for word in words:
88
+ test_line = f"{current_line} {word}".strip()
89
+ bbox = draw.textbbox((0, 0), test_line, font=font)
90
+ if bbox[2] - bbox[0] <= width:
91
+ current_line = test_line
92
+ else:
93
+ lines.append(current_line)
94
+ current_line = word
95
+ if current_line:
96
+ lines.append(current_line)
97
+
98
+ # Draw each line of text within the bounding box
99
+ line_height = draw.textbbox((0, 0), "Ag", font=font)[3] - draw.textbbox((0, 0), "Ag", font=font)[1]
100
+ start_y = (height - line_height * len(lines)) // 2
101
+ for i, line in enumerate(lines):
102
+ text_width = draw.textbbox((0, 0), line, font=font)[2] - draw.textbbox((0, 0), line, font=font)[0]
103
+ text_x = (width - text_width) // 2
104
+ text_y = start_y + i * line_height
105
+ draw.text((text_x, text_y), line, fill=(0, 0, 0, 255), font=font)
106
 
107
  # Convert back to OpenCV and reapply the perspective transformation
108
  warped_with_text = cv2.cvtColor(np.array(pil_warped), cv2.COLOR_RGBA2BGRA)
 
160
  interface.launch(share=True)
161
 
162
 
163
+