Commit
Β·
34a5af9
1
Parent(s):
c166286
Enhance layoutjson2md function: embed images in markdown output using base64 encoding for 'Picture' category
Browse files
app.py
CHANGED
@@ -210,6 +210,9 @@ def is_arabic_text(text: str) -> bool:
|
|
210 |
|
211 |
def layoutjson2md(image: Image.Image, layout_data: List[Dict], text_key: str = 'text') -> str:
|
212 |
"""Convert layout JSON to markdown format"""
|
|
|
|
|
|
|
213 |
markdown_lines = []
|
214 |
|
215 |
try:
|
@@ -219,11 +222,38 @@ def layoutjson2md(image: Image.Image, layout_data: List[Dict], text_key: str = '
|
|
219 |
for item in sorted_items:
|
220 |
category = item.get('category', '')
|
221 |
text = item.get(text_key, '')
|
|
|
222 |
|
223 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
continue
|
225 |
-
|
226 |
-
if category == 'Title':
|
227 |
markdown_lines.append(f"# {text}\n")
|
228 |
elif category == 'Section-header':
|
229 |
markdown_lines.append(f"## {text}\n")
|
|
|
210 |
|
211 |
def layoutjson2md(image: Image.Image, layout_data: List[Dict], text_key: str = 'text') -> str:
|
212 |
"""Convert layout JSON to markdown format"""
|
213 |
+
import base64
|
214 |
+
from io import BytesIO
|
215 |
+
|
216 |
markdown_lines = []
|
217 |
|
218 |
try:
|
|
|
222 |
for item in sorted_items:
|
223 |
category = item.get('category', '')
|
224 |
text = item.get(text_key, '')
|
225 |
+
bbox = item.get('bbox', [])
|
226 |
|
227 |
+
if category == 'Picture':
|
228 |
+
# Extract image region and embed it
|
229 |
+
if bbox and len(bbox) == 4:
|
230 |
+
try:
|
231 |
+
# Extract the image region
|
232 |
+
x1, y1, x2, y2 = bbox
|
233 |
+
# Ensure coordinates are within image bounds
|
234 |
+
x1, y1 = max(0, int(x1)), max(0, int(y1))
|
235 |
+
x2, y2 = min(image.width, int(x2)), min(image.height, int(y2))
|
236 |
+
|
237 |
+
if x2 > x1 and y2 > y1:
|
238 |
+
cropped_img = image.crop((x1, y1, x2, y2))
|
239 |
+
|
240 |
+
# Convert to base64 for embedding
|
241 |
+
buffer = BytesIO()
|
242 |
+
cropped_img.save(buffer, format='PNG')
|
243 |
+
img_data = base64.b64encode(buffer.getvalue()).decode()
|
244 |
+
|
245 |
+
# Add as markdown image
|
246 |
+
markdown_lines.append(f"\n")
|
247 |
+
else:
|
248 |
+
markdown_lines.append("\n")
|
249 |
+
except Exception as e:
|
250 |
+
print(f"Error processing image region: {e}")
|
251 |
+
markdown_lines.append("\n")
|
252 |
+
else:
|
253 |
+
markdown_lines.append("\n")
|
254 |
+
elif not text:
|
255 |
continue
|
256 |
+
elif category == 'Title':
|
|
|
257 |
markdown_lines.append(f"# {text}\n")
|
258 |
elif category == 'Section-header':
|
259 |
markdown_lines.append(f"## {text}\n")
|