File size: 1,378 Bytes
734b5b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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}