@@ -147,8 +147,7 @@ def add_ruler_edges(
147147def draw_bounding_boxes (
148148 image_data : bytes ,
149149 bboxes : list [dict ],
150- has_ruler : bool = False ,
151- ruler_width : int = 30 ,
150+ margin_offset : int = 0 ,
152151 box_color : str = "red" ,
153152 box_width : int = 3 ,
154153 label_font_size : int = 12 ,
@@ -160,12 +159,11 @@ def draw_bounding_boxes(
160159 Args:
161160 image_data: Raw image bytes
162161 bboxes: List of bounding box dictionaries, each containing:
163- - 'bbox': [x1, y1, x2, y2] in 0-1000 normalized scale
162+ - 'bbox': [x1, y1, x2, y2] in 0-1000 normalized scale (document space)
164163 - 'label': Optional label text for the box
165164 - 'color': Optional color override for this box
166165 - 'page': Optional page number (for multi-page docs)
167- has_ruler: If True, account for ruler margins in coordinate calculation
168- ruler_width: Width of ruler margin (only used if has_ruler=True)
166+ margin_offset: Pixel offset for top-left margin (e.g., if image has decorative margins)
169167 box_color: Default color for bounding boxes
170168 box_width: Line width for bounding boxes
171169 label_font_size: Font size for box labels
@@ -191,17 +189,11 @@ def draw_bounding_boxes(
191189 image = Image .open (io .BytesIO (image_data )).convert ("RGBA" )
192190 width , height = image .size
193191
194- # If image has ruler edges, calculate the actual document area
195- if has_ruler :
196- doc_width = width - ruler_width
197- doc_height = height - ruler_width
198- offset_x = ruler_width
199- offset_y = ruler_width
200- else :
201- doc_width = width
202- doc_height = height
203- offset_x = 0
204- offset_y = 0
192+ # Calculate document area (excluding any margin offset)
193+ doc_width = width - margin_offset
194+ doc_height = height - margin_offset
195+ offset_x = margin_offset
196+ offset_y = margin_offset
205197
206198 # Create overlay for semi-transparent boxes
207199 overlay = Image .new ("RGBA" , (width , height ), (0 , 0 , 0 , 0 ))
@@ -331,12 +323,11 @@ def add_ruler_and_draw_boxes(
331323 label_interval = label_interval ,
332324 )
333325
334- # Then draw bounding boxes (accounting for ruler offset)
326+ # Then draw bounding boxes (accounting for margin offset from ruler )
335327 result = draw_bounding_boxes (
336328 image_with_ruler ,
337329 bboxes ,
338- has_ruler = True ,
339- ruler_width = ruler_width ,
330+ margin_offset = ruler_width ,
340331 box_color = box_color ,
341332 box_width = box_width ,
342333 )
0 commit comments