modest improvement to cropping (safer)

This commit is contained in:
2026-09-15 15:13:41 +02:00
parent 9b22a8a137
commit 5b8215e7f5
6 changed files with 56 additions and 9 deletions
+14
View File
@@ -108,6 +108,14 @@ class InkDetectionTests(unittest.TestCase):
.7,(190,190,190),2)
self.assertGreater(detect_bounds(image,dpi=150)['bottom_px'],1100)
def test_sparse_central_pencil_on_dark_grid_survives(self):
image = self.dark_grid()
# Thin, pale handwriting crossing the ruling is split into sparse
# components. Its central position distinguishes it from page holes.
cv2.putText(image,'result',(330,1090),cv2.FONT_HERSHEY_SCRIPT_SIMPLEX,
.85,(155,155,155),1)
self.assertGreater(detect_bounds(image,dpi=150)['bottom_px'],1090)
def test_large_unruled_diagram_is_not_paper(self):
image = np.full((1200,850,3),255,np.uint8)
cv2.rectangle(image,(100,100),(750,1100),(20,20,20),3)
@@ -151,6 +159,12 @@ class InkDetectionTests(unittest.TestCase):
r = detect_bounds(image,dpi=150)
self.assertGreater(r['bottom_px'],1060)
def test_faint_page_number_in_outer_band_does_not_block_crop(self):
image = self.dark_grid()
cv2.putText(image,'4/',(3,1160),cv2.FONT_HERSHEY_SIMPLEX,
.55,(130,130,130),1)
self.assertLess(detect_bounds(image,dpi=150)['bottom_px'],850)
def test_faded_neutral_grid(self):
image = self.dark_grid()
image[np.all(image == 65,axis=2)] = 145
+7 -3
View File
@@ -8,7 +8,10 @@ import numpy as np
import pymupdf
from pdf2image import convert_from_path
from copienator.commands.splitting_int import _render_split_outputs
from copienator.commands.splitting_int import (
ANSWER_TOP_PADDING_POINTS,
_render_split_outputs,
)
class SplittingGeometryTests(unittest.TestCase):
@@ -60,7 +63,8 @@ class SplittingGeometryTests(unittest.TestCase):
else:
coordinates = [("A", 0, 250, 260, 0, 100),
("_", 0, 711, 721, 0, 100)]
bounds = (bounds[0], max(bounds[1], height // 4),
answer_top = int(height // 4 - ANSWER_TOP_PADDING_POINTS)
bounds = (bounds[0], max(bounds[1], answer_top),
bounds[2], min(bounds[3], height * 3 // 4))
_render_split_outputs(source, coordinates, root)
self.assert_matches_preview(root / "A.pdf", preview, bounds)
@@ -85,7 +89,7 @@ class SplittingGeometryTests(unittest.TestCase):
rendered = convert_from_path(root / "A.pdf", dpi=72)
self.assertEqual(len(rendered), 2)
for actual, preview, bounds in zip(rendered, previews,
[(40, 700, 780, 920), (20, 80, 760, 250)]):
[(40, 688, 780, 920), (20, 80, 760, 250)]):
expected = np.asarray(preview.crop(bounds)).astype(float)
actual = np.asarray(actual).astype(float)
self.assertEqual(actual.shape, expected.shape)