|
|
@@ -11,9 +11,6 @@ from src.split.slicer import (
|
|
11
|
11
|
SUPPORTED_EXTENSIONS,
|
|
12
|
12
|
VALID_ROTATIONS,
|
|
13
|
13
|
add_border,
|
|
14
|
|
- crop_to_content,
|
|
15
|
|
- detect_grid,
|
|
16
|
|
- find_content_bounds,
|
|
17
|
14
|
generate_output_paths,
|
|
18
|
15
|
load_image,
|
|
19
|
16
|
parse_slice,
|
|
|
@@ -22,18 +19,7 @@ from src.split.slicer import (
|
|
22
|
19
|
save_page,
|
|
23
|
20
|
slice_grid,
|
|
24
|
21
|
)
|
|
25
|
|
-
|
|
26
|
|
-
|
|
27
|
|
-def _create_test_image(width: int = 400, height: int = 300) -> np.ndarray:
|
|
28
|
|
- image = np.zeros((height, width, 3), dtype=np.uint8)
|
|
29
|
|
- image[:, :] = (200, 200, 200)
|
|
30
|
|
- return image
|
|
31
|
|
-
|
|
32
|
|
-
|
|
33
|
|
-def _image_to_path(image: np.ndarray, suffix: str = ".png") -> Path:
|
|
34
|
|
- with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
|
|
35
|
|
- cv2.imwrite(tmp.name, image)
|
|
36
|
|
- return Path(tmp.name)
|
|
|
22
|
+from tests.helpers import create_test_image, image_to_path
|
|
37
|
23
|
|
|
38
|
24
|
|
|
39
|
25
|
class TestParseSlice:
|
|
|
@@ -84,7 +70,7 @@ class TestParseSlice:
|
|
84
|
70
|
|
|
85
|
71
|
class TestSliceGrid:
|
|
86
|
72
|
def test_2x2_exact(self) -> None:
|
|
87
|
|
- image = _create_test_image(400, 300)
|
|
|
73
|
+ image = create_test_image(400, 300)
|
|
88
|
74
|
pages = slice_grid(image, 2, 2)
|
|
89
|
75
|
assert len(pages) == 4
|
|
90
|
76
|
assert pages[0].shape == (150, 200, 3)
|
|
|
@@ -93,34 +79,34 @@ class TestSliceGrid:
|
|
93
|
79
|
assert pages[3].shape == (150, 200, 3)
|
|
94
|
80
|
|
|
95
|
81
|
def test_1x1(self) -> None:
|
|
96
|
|
- image = _create_test_image(100, 100)
|
|
|
82
|
+ image = create_test_image(100, 100)
|
|
97
|
83
|
pages = slice_grid(image, 1, 1)
|
|
98
|
84
|
assert len(pages) == 1
|
|
99
|
85
|
assert pages[0].shape == (100, 100, 3)
|
|
100
|
86
|
|
|
101
|
87
|
def test_2x3(self) -> None:
|
|
102
|
|
- image = _create_test_image(300, 200)
|
|
|
88
|
+ image = create_test_image(300, 200)
|
|
103
|
89
|
pages = slice_grid(image, 2, 3)
|
|
104
|
90
|
assert len(pages) == 6
|
|
105
|
91
|
for page in pages:
|
|
106
|
92
|
assert page.shape == (100, 100, 3)
|
|
107
|
93
|
|
|
108
|
94
|
def test_uneven_division_width(self) -> None:
|
|
109
|
|
- image = _create_test_image(width=403, height=300)
|
|
|
95
|
+ image = create_test_image(width=403, height=300)
|
|
110
|
96
|
pages = slice_grid(image, 1, 2)
|
|
111
|
97
|
assert len(pages) == 2
|
|
112
|
98
|
assert pages[0].shape == (300, 201, 3)
|
|
113
|
99
|
assert pages[1].shape == (300, 202, 3)
|
|
114
|
100
|
|
|
115
|
101
|
def test_uneven_division_height(self) -> None:
|
|
116
|
|
- image = _create_test_image(width=400, height=301)
|
|
|
102
|
+ image = create_test_image(width=400, height=301)
|
|
117
|
103
|
pages = slice_grid(image, 2, 1)
|
|
118
|
104
|
assert len(pages) == 2
|
|
119
|
105
|
assert pages[0].shape == (150, 400, 3)
|
|
120
|
106
|
assert pages[1].shape == (151, 400, 3)
|
|
121
|
107
|
|
|
122
|
108
|
def test_uneven_both(self) -> None:
|
|
123
|
|
- image = _create_test_image(width=401, height=301)
|
|
|
109
|
+ image = create_test_image(width=401, height=301)
|
|
124
|
110
|
pages = slice_grid(image, 2, 2)
|
|
125
|
111
|
assert len(pages) == 4
|
|
126
|
112
|
assert pages[0].shape == (150, 200, 3)
|
|
|
@@ -137,13 +123,13 @@ class TestSliceGrid:
|
|
137
|
123
|
assert np.array_equal(image[50:100, 50:100], pages[3])
|
|
138
|
124
|
|
|
139
|
125
|
def test_total_pixels_preserved(self) -> None:
|
|
140
|
|
- image = _create_test_image(width=401, height=301)
|
|
|
126
|
+ image = create_test_image(width=401, height=301)
|
|
141
|
127
|
pages = slice_grid(image, 3, 3)
|
|
142
|
128
|
total = sum(page.shape[0] * page.shape[1] for page in pages)
|
|
143
|
129
|
assert total == 401 * 301
|
|
144
|
130
|
|
|
145
|
131
|
def test_order_2x2(self) -> None:
|
|
146
|
|
- image = _create_test_image(200, 200)
|
|
|
132
|
+ image = create_test_image(200, 200)
|
|
147
|
133
|
image[0:100, 0:100] = (255, 0, 0)
|
|
148
|
134
|
image[0:100, 100:200] = (0, 255, 0)
|
|
149
|
135
|
image[100:200, 0:100] = (0, 0, 255)
|
|
|
@@ -155,14 +141,14 @@ class TestSliceGrid:
|
|
155
|
141
|
assert pages[3][0, 0].tolist() == [255, 255, 0]
|
|
156
|
142
|
|
|
157
|
143
|
def test_order_2x4(self) -> None:
|
|
158
|
|
- image = _create_test_image(400, 200)
|
|
|
144
|
+ image = create_test_image(400, 200)
|
|
159
|
145
|
pages = slice_grid(image, 2, 4)
|
|
160
|
146
|
assert len(pages) == 8
|
|
161
|
147
|
assert pages[0].shape == (100, 100, 3)
|
|
162
|
148
|
assert pages[7].shape == (100, 100, 3)
|
|
163
|
149
|
|
|
164
|
150
|
def test_4x4(self) -> None:
|
|
165
|
|
- image = _create_test_image(400, 400)
|
|
|
151
|
+ image = create_test_image(400, 400)
|
|
166
|
152
|
pages = slice_grid(image, 4, 4)
|
|
167
|
153
|
assert len(pages) == 16
|
|
168
|
154
|
for page in pages:
|
|
|
@@ -171,17 +157,17 @@ class TestSliceGrid:
|
|
171
|
157
|
|
|
172
|
158
|
class TestAddBorder:
|
|
173
|
159
|
def test_default_border(self) -> None:
|
|
174
|
|
- image = _create_test_image(100, 100)
|
|
|
160
|
+ image = create_test_image(100, 100)
|
|
175
|
161
|
result = add_border(image, 20)
|
|
176
|
162
|
assert result.shape == (140, 140, 3)
|
|
177
|
163
|
|
|
178
|
164
|
def test_zero_border(self) -> None:
|
|
179
|
|
- image = _create_test_image(100, 100)
|
|
|
165
|
+ image = create_test_image(100, 100)
|
|
180
|
166
|
result = add_border(image, 0)
|
|
181
|
167
|
assert np.array_equal(result, image)
|
|
182
|
168
|
|
|
183
|
169
|
def test_border_is_white(self) -> None:
|
|
184
|
|
- image = _create_test_image(100, 100)
|
|
|
170
|
+ image = create_test_image(100, 100)
|
|
185
|
171
|
border = 10
|
|
186
|
172
|
result = add_border(image, border)
|
|
187
|
173
|
assert np.all(result[0:border, :] == 255)
|
|
|
@@ -190,34 +176,34 @@ class TestAddBorder:
|
|
190
|
176
|
assert np.all(result[:, -border:] == 255)
|
|
191
|
177
|
|
|
192
|
178
|
def test_large_border(self) -> None:
|
|
193
|
|
- image = _create_test_image(10, 10)
|
|
|
179
|
+ image = create_test_image(10, 10)
|
|
194
|
180
|
result = add_border(image, 50)
|
|
195
|
181
|
assert result.shape == (110, 110, 3)
|
|
196
|
182
|
|
|
197
|
183
|
|
|
198
|
184
|
class TestRotateImage:
|
|
199
|
185
|
def test_rotate_90(self) -> None:
|
|
200
|
|
- image = _create_test_image(200, 100)
|
|
|
186
|
+ image = create_test_image(200, 100)
|
|
201
|
187
|
result = rotate_image(image, 90)
|
|
202
|
188
|
assert result.shape == (200, 100, 3)
|
|
203
|
189
|
|
|
204
|
190
|
def test_rotate_180(self) -> None:
|
|
205
|
|
- image = _create_test_image(200, 100)
|
|
|
191
|
+ image = create_test_image(200, 100)
|
|
206
|
192
|
result = rotate_image(image, 180)
|
|
207
|
193
|
assert result.shape == (100, 200, 3)
|
|
208
|
194
|
|
|
209
|
195
|
def test_rotate_270(self) -> None:
|
|
210
|
|
- image = _create_test_image(200, 100)
|
|
|
196
|
+ image = create_test_image(200, 100)
|
|
211
|
197
|
result = rotate_image(image, 270)
|
|
212
|
198
|
assert result.shape == (200, 100, 3)
|
|
213
|
199
|
|
|
214
|
200
|
def test_invalid_angle(self) -> None:
|
|
215
|
|
- image = _create_test_image()
|
|
|
201
|
+ image = create_test_image()
|
|
216
|
202
|
with pytest.raises(ValueError, match="Недопустимый угол поворота"):
|
|
217
|
203
|
rotate_image(image, 45)
|
|
218
|
204
|
|
|
219
|
205
|
def test_invalid_angle_zero(self) -> None:
|
|
220
|
|
- image = _create_test_image()
|
|
|
206
|
+ image = create_test_image()
|
|
221
|
207
|
with pytest.raises(ValueError):
|
|
222
|
208
|
rotate_image(image, 0)
|
|
223
|
209
|
|
|
|
@@ -260,20 +246,20 @@ class TestGenerateOutputPaths:
|
|
260
|
246
|
|
|
261
|
247
|
class TestLoadImage:
|
|
262
|
248
|
def test_valid_jpeg(self) -> None:
|
|
263
|
|
- image = _create_test_image()
|
|
264
|
|
- path = _image_to_path(image, ".jpg")
|
|
|
249
|
+ image = create_test_image()
|
|
|
250
|
+ path = image_to_path(image, ".jpg")
|
|
265
|
251
|
loaded = load_image(path)
|
|
266
|
252
|
assert loaded.shape == (300, 400, 3)
|
|
267
|
253
|
|
|
268
|
254
|
def test_valid_png(self) -> None:
|
|
269
|
|
- image = _create_test_image()
|
|
270
|
|
- path = _image_to_path(image, ".png")
|
|
|
255
|
+ image = create_test_image()
|
|
|
256
|
+ path = image_to_path(image, ".png")
|
|
271
|
257
|
loaded = load_image(path)
|
|
272
|
258
|
assert loaded.shape == (300, 400, 3)
|
|
273
|
259
|
|
|
274
|
260
|
def test_valid_tiff(self) -> None:
|
|
275
|
|
- image = _create_test_image()
|
|
276
|
|
- path = _image_to_path(image, ".tiff")
|
|
|
261
|
+ image = create_test_image()
|
|
|
262
|
+ path = image_to_path(image, ".tiff")
|
|
277
|
263
|
loaded = load_image(path)
|
|
278
|
264
|
assert loaded.shape == (300, 400, 3)
|
|
279
|
265
|
|
|
|
@@ -297,7 +283,7 @@ class TestLoadImage:
|
|
297
|
283
|
|
|
298
|
284
|
class TestSavePage:
|
|
299
|
285
|
def test_saves_file(self) -> None:
|
|
300
|
|
- image = _create_test_image()
|
|
|
286
|
+ image = create_test_image()
|
|
301
|
287
|
with tempfile.TemporaryDirectory() as tmp:
|
|
302
|
288
|
path = Path(tmp) / "test.png"
|
|
303
|
289
|
save_page(image, path)
|
|
|
@@ -307,7 +293,7 @@ class TestSavePage:
|
|
307
|
293
|
assert loaded.shape == (300, 400, 3)
|
|
308
|
294
|
|
|
309
|
295
|
def test_creates_parent_dir(self) -> None:
|
|
310
|
|
- image = _create_test_image()
|
|
|
296
|
+ image = create_test_image()
|
|
311
|
297
|
with tempfile.TemporaryDirectory() as tmp:
|
|
312
|
298
|
path = Path(tmp) / "subdir" / "test.png"
|
|
313
|
299
|
save_page(image, path)
|
|
|
@@ -316,8 +302,8 @@ class TestSavePage:
|
|
316
|
302
|
|
|
317
|
303
|
class TestProcessImage:
|
|
318
|
304
|
def test_end_to_end_2x2(self) -> None:
|
|
319
|
|
- image = _create_test_image(400, 300)
|
|
320
|
|
- path = _image_to_path(image, ".jpg")
|
|
|
305
|
+ image = create_test_image(400, 300)
|
|
|
306
|
+ path = image_to_path(image, ".jpg")
|
|
321
|
307
|
with tempfile.TemporaryDirectory() as tmp:
|
|
322
|
308
|
result = process_image(Path(path), 2, 2, Path(tmp))
|
|
323
|
309
|
assert len(result) == 4
|
|
|
@@ -327,8 +313,8 @@ class TestProcessImage:
|
|
327
|
313
|
assert loaded.shape == (250, 300, 3)
|
|
328
|
314
|
|
|
329
|
315
|
def test_end_to_end_with_border(self) -> None:
|
|
330
|
|
- image = _create_test_image(200, 200)
|
|
331
|
|
- path = _image_to_path(image, ".png")
|
|
|
316
|
+ image = create_test_image(200, 200)
|
|
|
317
|
+ path = image_to_path(image, ".png")
|
|
332
|
318
|
with tempfile.TemporaryDirectory() as tmp:
|
|
333
|
319
|
result = process_image(Path(path), 2, 2, Path(tmp), border_px=10)
|
|
334
|
320
|
assert len(result) == 4
|
|
|
@@ -338,8 +324,8 @@ class TestProcessImage:
|
|
338
|
324
|
assert loaded.shape == (120, 120, 3)
|
|
339
|
325
|
|
|
340
|
326
|
def test_end_to_end_tiff(self) -> None:
|
|
341
|
|
- image = _create_test_image(400, 400)
|
|
342
|
|
- path = _image_to_path(image, ".tiff")
|
|
|
327
|
+ image = create_test_image(400, 400)
|
|
|
328
|
+ path = image_to_path(image, ".tiff")
|
|
343
|
329
|
with tempfile.TemporaryDirectory() as tmp:
|
|
344
|
330
|
result = process_image(Path(path), 2, 2, Path(tmp))
|
|
345
|
331
|
assert len(result) == 4
|
|
|
@@ -348,8 +334,8 @@ class TestProcessImage:
|
|
348
|
334
|
assert p.exists()
|
|
349
|
335
|
|
|
350
|
336
|
def test_end_to_end_with_rotation(self) -> None:
|
|
351
|
|
- image = _create_test_image(200, 400)
|
|
352
|
|
- path = _image_to_path(image, ".png")
|
|
|
337
|
+ image = create_test_image(200, 400)
|
|
|
338
|
+ path = image_to_path(image, ".png")
|
|
353
|
339
|
with tempfile.TemporaryDirectory() as tmp:
|
|
354
|
340
|
result = process_image(Path(path), 2, 2, Path(tmp), pre_rotate=90)
|
|
355
|
341
|
assert len(result) == 4
|
|
|
@@ -357,194 +343,3 @@ class TestProcessImage:
|
|
357
|
343
|
assert p.exists()
|
|
358
|
344
|
loaded = cv2.imread(str(result[0]))
|
|
359
|
345
|
assert loaded.shape[0] > 0
|
|
360
|
|
-
|
|
361
|
|
-
|
|
362
|
|
-class TestFindContentBounds:
|
|
363
|
|
- def test_uniform_image(self) -> None:
|
|
364
|
|
- image = _create_test_image(200, 150)
|
|
365
|
|
- image[:, :] = (255, 255, 255)
|
|
366
|
|
- x1, y1, x2, y2 = find_content_bounds(image)
|
|
367
|
|
- assert (x1, y1, x2, y2) == (0, 0, 200, 150)
|
|
368
|
|
-
|
|
369
|
|
- def test_single_dot_center(self) -> None:
|
|
370
|
|
- image = _create_test_image(200, 150)
|
|
371
|
|
- image[:, :] = (255, 255, 255)
|
|
372
|
|
- image[75, 100] = (0, 0, 0)
|
|
373
|
|
- x1, y1, x2, y2 = find_content_bounds(image)
|
|
374
|
|
- assert abs(x1 - 100) <= 1 and abs(x2 - 100) <= 1
|
|
375
|
|
- assert abs(y1 - 75) <= 1 and abs(y2 - 75) <= 1
|
|
376
|
|
-
|
|
377
|
|
- def test_rectangle_content(self) -> None:
|
|
378
|
|
- image = _create_test_image(200, 150)
|
|
379
|
|
- image[:, :] = (255, 255, 255)
|
|
380
|
|
- image[30:100, 50:140] = (0, 0, 0)
|
|
381
|
|
- x1, y1, x2, y2 = find_content_bounds(image)
|
|
382
|
|
- assert x1 == 50
|
|
383
|
|
- assert y1 == 30
|
|
384
|
|
- assert x2 == 139
|
|
385
|
|
- assert y2 == 99
|
|
386
|
|
-
|
|
387
|
|
- def test_content_at_edges(self) -> None:
|
|
388
|
|
- image = _create_test_image(200, 150)
|
|
389
|
|
- image[:, :] = (255, 255, 255)
|
|
390
|
|
- image[0:10, :] = (0, 0, 0)
|
|
391
|
|
- image[:, 0:10] = (0, 0, 0)
|
|
392
|
|
- x1, y1, _x2, _y2 = find_content_bounds(image)
|
|
393
|
|
- assert y1 == 0
|
|
394
|
|
- assert x1 == 0
|
|
395
|
|
-
|
|
396
|
|
- def test_grayscale_content(self) -> None:
|
|
397
|
|
- image = _create_test_image(100, 100)
|
|
398
|
|
- image[:, :] = (255, 255, 255)
|
|
399
|
|
- image[20:30, 20:30] = (128, 128, 128)
|
|
400
|
|
- x1, y1, x2, y2 = find_content_bounds(image)
|
|
401
|
|
- assert x1 <= 29 and x2 >= 20
|
|
402
|
|
- assert y1 <= 29 and y2 >= 20
|
|
403
|
|
-
|
|
404
|
|
-
|
|
405
|
|
-class TestCropToContent:
|
|
406
|
|
- def test_crop_centered_content(self) -> None:
|
|
407
|
|
- image = _create_test_image(200, 150)
|
|
408
|
|
- image[:, :] = (255, 255, 255)
|
|
409
|
|
- image[30:50, 40:60] = (0, 0, 0)
|
|
410
|
|
- result = crop_to_content(image, border_px=10)
|
|
411
|
|
- assert result.shape[0] >= 20
|
|
412
|
|
- assert result.shape[1] >= 20
|
|
413
|
|
-
|
|
414
|
|
- def test_crop_uniform_image_no_change(self) -> None:
|
|
415
|
|
- image = _create_test_image(100, 100)
|
|
416
|
|
- image[:, :] = (255, 255, 255)
|
|
417
|
|
- result = crop_to_content(image, border_px=10)
|
|
418
|
|
- assert result.shape == (100, 100, 3)
|
|
419
|
|
-
|
|
420
|
|
- def test_border_capped_by_image_edge(self) -> None:
|
|
421
|
|
- image = _create_test_image(100, 100)
|
|
422
|
|
- image[:, :] = (255, 255, 255)
|
|
423
|
|
- image[10:30, 10:30] = (0, 0, 0)
|
|
424
|
|
- result = crop_to_content(image, border_px=200)
|
|
425
|
|
- assert result.shape == (100, 100, 3)
|
|
426
|
|
-
|
|
427
|
|
- def test_border_equals_distance_to_edge(self) -> None:
|
|
428
|
|
- image = _create_test_image(100, 100)
|
|
429
|
|
- image[:, :] = (255, 255, 255)
|
|
430
|
|
- image[40:60, 30:70] = (0, 0, 0)
|
|
431
|
|
- result = crop_to_content(image, border_px=50)
|
|
432
|
|
- assert result.shape[1] <= 100
|
|
433
|
|
- assert result.shape[0] <= 100
|
|
434
|
|
-
|
|
435
|
|
- def test_dark_bg_no_crop_uniform(self) -> None:
|
|
436
|
|
- image = _create_test_image(100, 100)
|
|
437
|
|
- image[:, :] = (0, 0, 0)
|
|
438
|
|
- result = crop_to_content(image, border_px=0)
|
|
439
|
|
- assert result.shape == (100, 100, 3)
|
|
440
|
|
-
|
|
441
|
|
- def test_zero_border_exact_crop(self) -> None:
|
|
442
|
|
- image = _create_test_image(200, 150)
|
|
443
|
|
- image[:, :] = (255, 255, 255)
|
|
444
|
|
- image[20:100, 30:170] = (0, 0, 0)
|
|
445
|
|
- result = crop_to_content(image, border_px=0)
|
|
446
|
|
- assert result.shape == (80, 140, 3)
|
|
447
|
|
-
|
|
448
|
|
-
|
|
449
|
|
-class TestProcessImagePostCrop:
|
|
450
|
|
- def test_post_crop_reduces_size(self) -> None:
|
|
451
|
|
- image = _create_test_image(400, 400)
|
|
452
|
|
- image[:, :] = (255, 255, 255)
|
|
453
|
|
- image[100:200, 100:200] = (0, 0, 0)
|
|
454
|
|
- path = _image_to_path(image, ".png")
|
|
455
|
|
- with tempfile.TemporaryDirectory() as tmp:
|
|
456
|
|
- result = process_image(Path(path), 1, 1, Path(tmp), border_px=30, post_crop=True)
|
|
457
|
|
- assert len(result) == 1
|
|
458
|
|
- loaded = cv2.imread(str(result[0]))
|
|
459
|
|
- assert loaded.shape[0] < 400 or loaded.shape[1] < 400
|
|
460
|
|
-
|
|
461
|
|
- def test_post_crop_off_no_crop(self) -> None:
|
|
462
|
|
- image = _create_test_image(400, 300)
|
|
463
|
|
- path = _image_to_path(image, ".png")
|
|
464
|
|
- with tempfile.TemporaryDirectory() as tmp:
|
|
465
|
|
- result = process_image(Path(path), 1, 1, Path(tmp), post_crop=False)
|
|
466
|
|
- assert len(result) == 1
|
|
467
|
|
- loaded = cv2.imread(str(result[0]))
|
|
468
|
|
- assert loaded.shape == (400, 500, 3)
|
|
469
|
|
-
|
|
470
|
|
- def test_post_crop_e2e_2x2(self) -> None:
|
|
471
|
|
- image = _create_test_image(400, 400)
|
|
472
|
|
- image[:, :] = (255, 255, 255)
|
|
473
|
|
- image[50:150, 50:150] = (0, 0, 0)
|
|
474
|
|
- image[50:150, 250:350] = (0, 0, 0)
|
|
475
|
|
- image[250:350, 50:150] = (0, 0, 0)
|
|
476
|
|
- image[250:350, 250:350] = (0, 0, 0)
|
|
477
|
|
- path = _image_to_path(image, ".png")
|
|
478
|
|
- with tempfile.TemporaryDirectory() as tmp:
|
|
479
|
|
- result_crop = process_image(Path(path), 2, 2, Path(tmp) / "crop", border_px=30, post_crop=True)
|
|
480
|
|
- result_no = process_image(Path(path), 2, 2, Path(tmp) / "no", border_px=30, post_crop=False)
|
|
481
|
|
- assert len(result_crop) == 4
|
|
482
|
|
- for cp, np in zip(result_crop, result_no):
|
|
483
|
|
- c = cv2.imread(str(cp))
|
|
484
|
|
- n = cv2.imread(str(np))
|
|
485
|
|
- assert c.shape[0] < n.shape[0]
|
|
486
|
|
- assert c.shape[1] < n.shape[1]
|
|
487
|
|
-
|
|
488
|
|
-
|
|
489
|
|
-class TestDetectGrid:
|
|
490
|
|
- def test_2x2(self) -> None:
|
|
491
|
|
- image = _create_test_image(400, 300)
|
|
492
|
|
- image[:, :] = (255, 255, 255)
|
|
493
|
|
- dark = (0, 0, 0)
|
|
494
|
|
- gap = 20
|
|
495
|
|
- bw = (400 - 3 * gap) // 2
|
|
496
|
|
- bh = (300 - 3 * gap) // 2
|
|
497
|
|
- for r in range(2):
|
|
498
|
|
- for c in range(2):
|
|
499
|
|
- y = gap + r * (bh + gap)
|
|
500
|
|
- x = gap + c * (bw + gap)
|
|
501
|
|
- image[y:y + bh, x:x + bw] = dark
|
|
502
|
|
- cols, rows = detect_grid(image)
|
|
503
|
|
- assert cols == 2
|
|
504
|
|
- assert rows == 2
|
|
505
|
|
-
|
|
506
|
|
- def test_2x4(self) -> None:
|
|
507
|
|
- image = _create_test_image(800, 400)
|
|
508
|
|
- image[:, :] = (255, 255, 255)
|
|
509
|
|
- gap = 15
|
|
510
|
|
- bw = (800 - 5 * gap) // 4
|
|
511
|
|
- bh = (400 - 3 * gap) // 2
|
|
512
|
|
- dark = (0, 0, 0)
|
|
513
|
|
- for r in range(2):
|
|
514
|
|
- for c in range(4):
|
|
515
|
|
- y = gap + r * (bh + gap)
|
|
516
|
|
- x = gap + c * (bw + gap)
|
|
517
|
|
- image[y:y + bh, x:x + bw] = dark
|
|
518
|
|
- cols, rows = detect_grid(image)
|
|
519
|
|
- assert cols == 4
|
|
520
|
|
- assert rows == 2
|
|
521
|
|
-
|
|
522
|
|
- def test_1x1(self) -> None:
|
|
523
|
|
- image = _create_test_image(200, 200)
|
|
524
|
|
- image[:, :] = (255, 255, 255)
|
|
525
|
|
- image[20:180, 20:180] = (0, 0, 0)
|
|
526
|
|
- cols, rows = detect_grid(image)
|
|
527
|
|
- assert cols == 1
|
|
528
|
|
- assert rows == 1
|
|
529
|
|
-
|
|
530
|
|
- def test_3x3(self) -> None:
|
|
531
|
|
- image = _create_test_image(600, 450)
|
|
532
|
|
- image[:, :] = (255, 255, 255)
|
|
533
|
|
- dark = (0, 0, 0)
|
|
534
|
|
- gap = 10
|
|
535
|
|
- bw = (600 - 4 * gap) // 3
|
|
536
|
|
- bh = (450 - 4 * gap) // 3
|
|
537
|
|
- for r in range(3):
|
|
538
|
|
- for c in range(3):
|
|
539
|
|
- y = gap + r * (bh + gap)
|
|
540
|
|
- x = gap + c * (bw + gap)
|
|
541
|
|
- image[y:y + bh, x:x + bw] = dark
|
|
542
|
|
- cols, rows = detect_grid(image)
|
|
543
|
|
- assert cols == 3
|
|
544
|
|
- assert rows == 3
|
|
545
|
|
-
|
|
546
|
|
- def test_uniform_image(self) -> None:
|
|
547
|
|
- image = _create_test_image()
|
|
548
|
|
- cols, rows = detect_grid(image)
|
|
549
|
|
- assert cols == 1
|
|
550
|
|
- assert rows == 1
|