rianders commited on
Commit
ff719a1
·
1 Parent(s): e2bc80e

Fix overlay scaling mismatch by applying DPI scale factor to bounding boxes

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -261,15 +261,20 @@ def render_page_with_overlay(
261
 
262
  ordered = order_blocks(blocks, order_mode)
263
 
 
 
264
  # Try to use a default font; if not available, PIL will still draw text.
265
  try:
266
  font = ImageFont.load_default()
267
  except Exception:
268
  font = None
269
 
 
 
 
 
270
  for rank, (idx, b) in enumerate(ordered, start=1):
271
- x0, y0, x1, y1 = b.bbox
272
- r = _rect_i((x0, y0, x1, y1))
273
 
274
  is_text = (b.block_type == 0 and b.text.strip() != "")
275
  is_math = is_text and _looks_like_math(b.text)
@@ -285,8 +290,7 @@ def render_page_with_overlay(
285
 
286
  if show_spans and b.block_type == 0:
287
  for sp in b.spans:
288
- sx0, sy0, sx1, sy1 = sp.bbox
289
- sr = _rect_i((sx0, sy0, sx1, sy1))
290
  draw.rectangle(sr, outline=colors['span_border'], width=1)
291
 
292
  return img
 
261
 
262
  ordered = order_blocks(blocks, order_mode)
263
 
264
+ scale = dpi / 72.0
265
+
266
  # Try to use a default font; if not available, PIL will still draw text.
267
  try:
268
  font = ImageFont.load_default()
269
  except Exception:
270
  font = None
271
 
272
+ def _scale_rect(rect):
273
+ x0, y0, x1, y1 = rect
274
+ return (int(x0 * scale), int(y0 * scale), int(x1 * scale), int(y1 * scale))
275
+
276
  for rank, (idx, b) in enumerate(ordered, start=1):
277
+ r = _scale_rect(b.bbox)
 
278
 
279
  is_text = (b.block_type == 0 and b.text.strip() != "")
280
  is_math = is_text and _looks_like_math(b.text)
 
290
 
291
  if show_spans and b.block_type == 0:
292
  for sp in b.spans:
293
+ sr = _scale_rect(sp.bbox)
 
294
  draw.rectangle(sr, outline=colors['span_border'], width=1)
295
 
296
  return img