techprotrade's picture
Add src directory
734b5b4 verified
Raw
History Blame Contribute Delete
1.38 kB
from PIL import Image
from typing import Dict, List, Any
import io
class LayoutAnalysisAgent:
"""Analyzes PDF page layout - columns, headers, footers, margins."""
def process(self, pdf_page: Image.Image) -> Dict[str, Any]:
"""Analyze layout structure."""
width, height = pdf_page.size
return {
"dimensions": {"width": width, "height": height},
"regions": self._detect_regions(width, height),
"columns": self._detect_columns(pdf_page),
"margins": self._analyze_margins(pdf_page),
}
def _detect_regions(self, width: int, height: int) -> Dict[str, Dict]:
"""Detect page regions."""
return {
"header": {"y_start": 0, "y_end": int(height * 0.1)},
"body": {"y_start": int(height * 0.1), "y_end": int(height * 0.9)},
"footer": {"y_start": int(height * 0.9), "y_end": height},
}
def _detect_columns(self, image: Image.Image) -> List[Dict]:
"""Detect text columns."""
width = image.size[0]
return [
{"id": 0, "x_start": 0, "x_end": width // 2},
{"id": 1, "x_start": width // 2, "x_end": width},
]
def _analyze_margins(self, image: Image.Image) -> Dict[str, int]:
"""Analyze page margins."""
return {"top": 50, "bottom": 50, "left": 50, "right": 50}