|
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+from __future__ import annotations
|
|
|
2
|
+
|
|
|
3
|
+import tempfile
|
|
|
4
|
+from pathlib import Path
|
|
|
5
|
+
|
|
|
6
|
+import cv2
|
|
|
7
|
+import numpy as np
|
|
|
8
|
+import pytest
|
|
|
9
|
+
|
|
|
10
|
+from src.split.slicer import (
|
|
|
11
|
+ SUPPORTED_EXTENSIONS,
|
|
|
12
|
+ VALID_ROTATIONS,
|
|
|
13
|
+ add_border,
|
|
|
14
|
+ crop_to_content,
|
|
|
15
|
+ find_content_bounds,
|
|
|
16
|
+ generate_output_paths,
|
|
|
17
|
+ load_image,
|
|
|
18
|
+ parse_slice,
|
|
|
19
|
+ process_image,
|
|
|
20
|
+ rotate_image,
|
|
|
21
|
+ save_page,
|
|
|
22
|
+ slice_grid,
|
|
|
23
|
+)
|
|
|
24
|
+
|
|
|
25
|
+
|
|
|
26
|
+def _create_test_image(width: int = 400, height: int = 300) -> np.ndarray:
|
|
|
27
|
+ image = np.zeros((height, width, 3), dtype=np.uint8)
|
|
|
28
|
+ image[:, :] = (200, 200, 200)
|
|
|
29
|
+ return image
|
|
|
30
|
+
|
|
|
31
|
+
|
|
|
32
|
+def _image_to_path(image: np.ndarray, suffix: str = ".png") -> Path:
|
|
|
33
|
+ with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
|
|
|
34
|
+ cv2.imwrite(tmp.name, image)
|
|
|
35
|
+ return Path(tmp.name)
|
|
|
36
|
+
|
|
|
37
|
+
|
|
|
38
|
+class TestParseSlice:
|
|
|
39
|
+ def test_simple_2x2(self) -> None:
|
|
|
40
|
+ assert parse_slice("2:2") == (2, 2)
|
|
|
41
|
+
|
|
|
42
|
+ def test_2x4(self) -> None:
|
|
|
43
|
+ assert parse_slice("2:4") == (2, 4)
|
|
|
44
|
+
|
|
|
45
|
+ def test_1x1(self) -> None:
|
|
|
46
|
+ assert parse_slice("1:1") == (1, 1)
|
|
|
47
|
+
|
|
|
48
|
+ def test_large(self) -> None:
|
|
|
49
|
+ assert parse_slice("10:10") == (10, 10)
|
|
|
50
|
+
|
|
|
51
|
+ def test_invalid_format_missing_colon(self) -> None:
|
|
|
52
|
+ with pytest.raises(ValueError, match="Неверный формат --slice"):
|
|
|
53
|
+ parse_slice("22")
|
|
|
54
|
+
|
|
|
55
|
+ def test_invalid_format_extra_colon(self) -> None:
|
|
|
56
|
+ with pytest.raises(ValueError, match="Неверный формат --slice"):
|
|
|
57
|
+ parse_slice("2:3:4")
|
|
|
58
|
+
|
|
|
59
|
+ def test_non_integer_value(self) -> None:
|
|
|
60
|
+ with pytest.raises(ValueError, match="Неверный формат --slice"):
|
|
|
61
|
+ parse_slice("a:b")
|
|
|
62
|
+
|
|
|
63
|
+ def test_non_integer_col(self) -> None:
|
|
|
64
|
+ with pytest.raises(ValueError, match="Неверный формат --slice"):
|
|
|
65
|
+ parse_slice("x:2")
|
|
|
66
|
+
|
|
|
67
|
+ def test_zero_cols(self) -> None:
|
|
|
68
|
+ with pytest.raises(ValueError, match="Неверное значение --slice"):
|
|
|
69
|
+ parse_slice("0:2")
|
|
|
70
|
+
|
|
|
71
|
+ def test_zero_rows(self) -> None:
|
|
|
72
|
+ with pytest.raises(ValueError, match="Неверное значение --slice"):
|
|
|
73
|
+ parse_slice("2:0")
|
|
|
74
|
+
|
|
|
75
|
+ def test_negative_cols(self) -> None:
|
|
|
76
|
+ with pytest.raises(ValueError, match="Неверное значение --slice"):
|
|
|
77
|
+ parse_slice("-1:2")
|
|
|
78
|
+
|
|
|
79
|
+ def test_blank_string(self) -> None:
|
|
|
80
|
+ with pytest.raises(ValueError):
|
|
|
81
|
+ parse_slice("")
|
|
|
82
|
+
|
|
|
83
|
+
|
|
|
84
|
+class TestSliceGrid:
|
|
|
85
|
+ def test_2x2_exact(self) -> None:
|
|
|
86
|
+ image = _create_test_image(400, 300)
|
|
|
87
|
+ pages = slice_grid(image, 2, 2)
|
|
|
88
|
+ assert len(pages) == 4
|
|
|
89
|
+ assert pages[0].shape == (150, 200, 3)
|
|
|
90
|
+ assert pages[1].shape == (150, 200, 3)
|
|
|
91
|
+ assert pages[2].shape == (150, 200, 3)
|
|
|
92
|
+ assert pages[3].shape == (150, 200, 3)
|
|
|
93
|
+
|
|
|
94
|
+ def test_1x1(self) -> None:
|
|
|
95
|
+ image = _create_test_image(100, 100)
|
|
|
96
|
+ pages = slice_grid(image, 1, 1)
|
|
|
97
|
+ assert len(pages) == 1
|
|
|
98
|
+ assert pages[0].shape == (100, 100, 3)
|
|
|
99
|
+
|
|
|
100
|
+ def test_2x3(self) -> None:
|
|
|
101
|
+ image = _create_test_image(300, 200)
|
|
|
102
|
+ pages = slice_grid(image, 2, 3)
|
|
|
103
|
+ assert len(pages) == 6
|
|
|
104
|
+ for page in pages:
|
|
|
105
|
+ assert page.shape == (100, 100, 3)
|
|
|
106
|
+
|
|
|
107
|
+ def test_uneven_division_width(self) -> None:
|
|
|
108
|
+ image = _create_test_image(width=403, height=300)
|
|
|
109
|
+ pages = slice_grid(image, 1, 2)
|
|
|
110
|
+ assert len(pages) == 2
|
|
|
111
|
+ assert pages[0].shape == (300, 201, 3)
|
|
|
112
|
+ assert pages[1].shape == (300, 202, 3)
|
|
|
113
|
+
|
|
|
114
|
+ def test_uneven_division_height(self) -> None:
|
|
|
115
|
+ image = _create_test_image(width=400, height=301)
|
|
|
116
|
+ pages = slice_grid(image, 2, 1)
|
|
|
117
|
+ assert len(pages) == 2
|
|
|
118
|
+ assert pages[0].shape == (150, 400, 3)
|
|
|
119
|
+ assert pages[1].shape == (151, 400, 3)
|
|
|
120
|
+
|
|
|
121
|
+ def test_uneven_both(self) -> None:
|
|
|
122
|
+ image = _create_test_image(width=401, height=301)
|
|
|
123
|
+ pages = slice_grid(image, 2, 2)
|
|
|
124
|
+ assert len(pages) == 4
|
|
|
125
|
+ assert pages[0].shape == (150, 200, 3)
|
|
|
126
|
+ assert pages[1].shape == (150, 201, 3)
|
|
|
127
|
+ assert pages[2].shape == (151, 200, 3)
|
|
|
128
|
+ assert pages[3].shape == (151, 201, 3)
|
|
|
129
|
+
|
|
|
130
|
+ def test_pixel_content_preserved(self) -> None:
|
|
|
131
|
+ image = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
|
|
|
132
|
+ pages = slice_grid(image, 2, 2)
|
|
|
133
|
+ assert np.array_equal(image[0:50, 0:50], pages[0])
|
|
|
134
|
+ assert np.array_equal(image[0:50, 50:100], pages[1])
|
|
|
135
|
+ assert np.array_equal(image[50:100, 0:50], pages[2])
|
|
|
136
|
+ assert np.array_equal(image[50:100, 50:100], pages[3])
|
|
|
137
|
+
|
|
|
138
|
+ def test_total_pixels_preserved(self) -> None:
|
|
|
139
|
+ image = _create_test_image(width=401, height=301)
|
|
|
140
|
+ pages = slice_grid(image, 3, 3)
|
|
|
141
|
+ total = sum(page.shape[0] * page.shape[1] for page in pages)
|
|
|
142
|
+ assert total == 401 * 301
|
|
|
143
|
+
|
|
|
144
|
+ def test_order_2x2(self) -> None:
|
|
|
145
|
+ image = _create_test_image(200, 200)
|
|
|
146
|
+ image[0:100, 0:100] = (255, 0, 0)
|
|
|
147
|
+ image[0:100, 100:200] = (0, 255, 0)
|
|
|
148
|
+ image[100:200, 0:100] = (0, 0, 255)
|
|
|
149
|
+ image[100:200, 100:200] = (255, 255, 0)
|
|
|
150
|
+ pages = slice_grid(image, 2, 2)
|
|
|
151
|
+ assert pages[0][0, 0].tolist() == [255, 0, 0]
|
|
|
152
|
+ assert pages[1][0, 0].tolist() == [0, 255, 0]
|
|
|
153
|
+ assert pages[2][0, 0].tolist() == [0, 0, 255]
|
|
|
154
|
+ assert pages[3][0, 0].tolist() == [255, 255, 0]
|
|
|
155
|
+
|
|
|
156
|
+ def test_order_2x4(self) -> None:
|
|
|
157
|
+ image = _create_test_image(400, 200)
|
|
|
158
|
+ pages = slice_grid(image, 2, 4)
|
|
|
159
|
+ assert len(pages) == 8
|
|
|
160
|
+ assert pages[0].shape == (100, 100, 3)
|
|
|
161
|
+ assert pages[7].shape == (100, 100, 3)
|
|
|
162
|
+
|
|
|
163
|
+ def test_4x4(self) -> None:
|
|
|
164
|
+ image = _create_test_image(400, 400)
|
|
|
165
|
+ pages = slice_grid(image, 4, 4)
|
|
|
166
|
+ assert len(pages) == 16
|
|
|
167
|
+ for page in pages:
|
|
|
168
|
+ assert page.shape == (100, 100, 3)
|
|
|
169
|
+
|
|
|
170
|
+
|
|
|
171
|
+class TestAddBorder:
|
|
|
172
|
+ def test_default_border(self) -> None:
|
|
|
173
|
+ image = _create_test_image(100, 100)
|
|
|
174
|
+ result = add_border(image, 20)
|
|
|
175
|
+ assert result.shape == (140, 140, 3)
|
|
|
176
|
+
|
|
|
177
|
+ def test_zero_border(self) -> None:
|
|
|
178
|
+ image = _create_test_image(100, 100)
|
|
|
179
|
+ result = add_border(image, 0)
|
|
|
180
|
+ assert np.array_equal(result, image)
|
|
|
181
|
+
|
|
|
182
|
+ def test_border_is_white(self) -> None:
|
|
|
183
|
+ image = _create_test_image(100, 100)
|
|
|
184
|
+ border = 10
|
|
|
185
|
+ result = add_border(image, border)
|
|
|
186
|
+ assert np.all(result[0:border, :] == 255)
|
|
|
187
|
+ assert np.all(result[-border:, :] == 255)
|
|
|
188
|
+ assert np.all(result[:, 0:border] == 255)
|
|
|
189
|
+ assert np.all(result[:, -border:] == 255)
|
|
|
190
|
+
|
|
|
191
|
+ def test_large_border(self) -> None:
|
|
|
192
|
+ image = _create_test_image(10, 10)
|
|
|
193
|
+ result = add_border(image, 50)
|
|
|
194
|
+ assert result.shape == (110, 110, 3)
|
|
|
195
|
+
|
|
|
196
|
+
|
|
|
197
|
+class TestRotateImage:
|
|
|
198
|
+ def test_rotate_90(self) -> None:
|
|
|
199
|
+ image = _create_test_image(200, 100)
|
|
|
200
|
+ result = rotate_image(image, 90)
|
|
|
201
|
+ assert result.shape == (200, 100, 3)
|
|
|
202
|
+
|
|
|
203
|
+ def test_rotate_180(self) -> None:
|
|
|
204
|
+ image = _create_test_image(200, 100)
|
|
|
205
|
+ result = rotate_image(image, 180)
|
|
|
206
|
+ assert result.shape == (100, 200, 3)
|
|
|
207
|
+
|
|
|
208
|
+ def test_rotate_270(self) -> None:
|
|
|
209
|
+ image = _create_test_image(200, 100)
|
|
|
210
|
+ result = rotate_image(image, 270)
|
|
|
211
|
+ assert result.shape == (200, 100, 3)
|
|
|
212
|
+
|
|
|
213
|
+ def test_invalid_angle(self) -> None:
|
|
|
214
|
+ image = _create_test_image()
|
|
|
215
|
+ with pytest.raises(ValueError, match="Недопустимый угол поворота"):
|
|
|
216
|
+ rotate_image(image, 45)
|
|
|
217
|
+
|
|
|
218
|
+ def test_invalid_angle_zero(self) -> None:
|
|
|
219
|
+ image = _create_test_image()
|
|
|
220
|
+ with pytest.raises(ValueError):
|
|
|
221
|
+ rotate_image(image, 0)
|
|
|
222
|
+
|
|
|
223
|
+ def test_valid_rotations_set(self) -> None:
|
|
|
224
|
+ assert VALID_ROTATIONS == frozenset({90, 180, 270})
|
|
|
225
|
+
|
|
|
226
|
+
|
|
|
227
|
+class TestGenerateOutputPaths:
|
|
|
228
|
+ def test_simple(self) -> None:
|
|
|
229
|
+ paths = generate_output_paths(Path("scan001.jpg"), 2, Path("/tmp"))
|
|
|
230
|
+ assert paths == [Path("/tmp/scan001_01.jpg"), Path("/tmp/scan001_02.jpg")]
|
|
|
231
|
+
|
|
|
232
|
+ def test_tiff_extension(self) -> None:
|
|
|
233
|
+ paths = generate_output_paths(Path("doc.tiff"), 3, Path("out"))
|
|
|
234
|
+ assert paths == [
|
|
|
235
|
+ Path("out/doc_01.tiff"),
|
|
|
236
|
+ Path("out/doc_02.tiff"),
|
|
|
237
|
+ Path("out/doc_03.tiff"),
|
|
|
238
|
+ ]
|
|
|
239
|
+
|
|
|
240
|
+ def test_png_extension(self) -> None:
|
|
|
241
|
+ paths = generate_output_paths(Path("img.PNG"), 1, Path("out"))
|
|
|
242
|
+ assert paths == [Path("out/img_01.png")]
|
|
|
243
|
+
|
|
|
244
|
+ def test_zero_padding(self) -> None:
|
|
|
245
|
+ paths = generate_output_paths(Path("scan.jpg"), 12, Path("out"))
|
|
|
246
|
+ assert paths[0] == Path("out/scan_01.jpg")
|
|
|
247
|
+ assert paths[9] == Path("out/scan_10.jpg")
|
|
|
248
|
+ assert paths[11] == Path("out/scan_12.jpg")
|
|
|
249
|
+
|
|
|
250
|
+ def test_many_pages(self) -> None:
|
|
|
251
|
+ paths = generate_output_paths(Path("scan.jpg"), 100, Path("out"))
|
|
|
252
|
+ assert paths[0] == Path("out/scan_001.jpg")
|
|
|
253
|
+ assert paths[99] == Path("out/scan_100.jpg")
|
|
|
254
|
+
|
|
|
255
|
+ def test_dot_in_filename(self) -> None:
|
|
|
256
|
+ paths = generate_output_paths(Path("scan.001.jpg"), 2, Path("out"))
|
|
|
257
|
+ assert paths == [Path("out/scan.001_01.jpg"), Path("out/scan.001_02.jpg")]
|
|
|
258
|
+
|
|
|
259
|
+
|
|
|
260
|
+class TestLoadImage:
|
|
|
261
|
+ def test_valid_jpeg(self) -> None:
|
|
|
262
|
+ image = _create_test_image()
|
|
|
263
|
+ path = _image_to_path(image, ".jpg")
|
|
|
264
|
+ loaded = load_image(path)
|
|
|
265
|
+ assert loaded.shape == (300, 400, 3)
|
|
|
266
|
+
|
|
|
267
|
+ def test_valid_png(self) -> None:
|
|
|
268
|
+ image = _create_test_image()
|
|
|
269
|
+ path = _image_to_path(image, ".png")
|
|
|
270
|
+ loaded = load_image(path)
|
|
|
271
|
+ assert loaded.shape == (300, 400, 3)
|
|
|
272
|
+
|
|
|
273
|
+ def test_valid_tiff(self) -> None:
|
|
|
274
|
+ image = _create_test_image()
|
|
|
275
|
+ path = _image_to_path(image, ".tiff")
|
|
|
276
|
+ loaded = load_image(path)
|
|
|
277
|
+ assert loaded.shape == (300, 400, 3)
|
|
|
278
|
+
|
|
|
279
|
+ def test_file_not_found(self) -> None:
|
|
|
280
|
+ with pytest.raises(FileNotFoundError, match="Файл не найден"):
|
|
|
281
|
+ load_image(Path("/tmp/nonexistent_scan2html_test.jpg"))
|
|
|
282
|
+
|
|
|
283
|
+ def test_unsupported_extension(self) -> None:
|
|
|
284
|
+ path = Path("/tmp/test.bmp")
|
|
|
285
|
+ with pytest.raises(ValueError, match="Неподдерживаемый формат"):
|
|
|
286
|
+ load_image(path)
|
|
|
287
|
+
|
|
|
288
|
+ def test_unsupported_extension_no_dot(self) -> None:
|
|
|
289
|
+ path = Path("/tmp/test")
|
|
|
290
|
+ with pytest.raises(ValueError, match="Неподдерживаемый формат"):
|
|
|
291
|
+ load_image(path)
|
|
|
292
|
+
|
|
|
293
|
+ def test_supported_extensions_set(self) -> None:
|
|
|
294
|
+ assert SUPPORTED_EXTENSIONS == frozenset({".jpg", ".jpeg", ".png", ".tiff", ".tif"})
|
|
|
295
|
+
|
|
|
296
|
+
|
|
|
297
|
+class TestSavePage:
|
|
|
298
|
+ def test_saves_file(self) -> None:
|
|
|
299
|
+ image = _create_test_image()
|
|
|
300
|
+ with tempfile.TemporaryDirectory() as tmp:
|
|
|
301
|
+ path = Path(tmp) / "test.png"
|
|
|
302
|
+ save_page(image, path)
|
|
|
303
|
+ assert path.exists()
|
|
|
304
|
+ loaded = cv2.imread(str(path))
|
|
|
305
|
+ assert loaded is not None
|
|
|
306
|
+ assert loaded.shape == (300, 400, 3)
|
|
|
307
|
+
|
|
|
308
|
+ def test_creates_parent_dir(self) -> None:
|
|
|
309
|
+ image = _create_test_image()
|
|
|
310
|
+ with tempfile.TemporaryDirectory() as tmp:
|
|
|
311
|
+ path = Path(tmp) / "subdir" / "test.png"
|
|
|
312
|
+ save_page(image, path)
|
|
|
313
|
+ assert path.exists()
|
|
|
314
|
+
|
|
|
315
|
+
|
|
|
316
|
+class TestProcessImage:
|
|
|
317
|
+ def test_end_to_end_2x2(self) -> None:
|
|
|
318
|
+ image = _create_test_image(400, 300)
|
|
|
319
|
+ path = _image_to_path(image, ".jpg")
|
|
|
320
|
+ with tempfile.TemporaryDirectory() as tmp:
|
|
|
321
|
+ result = process_image(Path(path), 2, 2, Path(tmp))
|
|
|
322
|
+ assert len(result) == 4
|
|
|
323
|
+ for p in result:
|
|
|
324
|
+ assert p.exists()
|
|
|
325
|
+ loaded = cv2.imread(str(p))
|
|
|
326
|
+ assert loaded.shape == (250, 300, 3)
|
|
|
327
|
+
|
|
|
328
|
+ def test_end_to_end_with_border(self) -> None:
|
|
|
329
|
+ image = _create_test_image(200, 200)
|
|
|
330
|
+ path = _image_to_path(image, ".png")
|
|
|
331
|
+ with tempfile.TemporaryDirectory() as tmp:
|
|
|
332
|
+ result = process_image(Path(path), 2, 2, Path(tmp), border_px=10)
|
|
|
333
|
+ assert len(result) == 4
|
|
|
334
|
+ for p in result:
|
|
|
335
|
+ assert p.exists()
|
|
|
336
|
+ loaded = cv2.imread(str(p))
|
|
|
337
|
+ assert loaded.shape == (120, 120, 3)
|
|
|
338
|
+
|
|
|
339
|
+ def test_end_to_end_tiff(self) -> None:
|
|
|
340
|
+ image = _create_test_image(400, 400)
|
|
|
341
|
+ path = _image_to_path(image, ".tiff")
|
|
|
342
|
+ with tempfile.TemporaryDirectory() as tmp:
|
|
|
343
|
+ result = process_image(Path(path), 2, 2, Path(tmp))
|
|
|
344
|
+ assert len(result) == 4
|
|
|
345
|
+ for p in result:
|
|
|
346
|
+ assert p.suffix.lower() == ".tiff"
|
|
|
347
|
+ assert p.exists()
|
|
|
348
|
+
|
|
|
349
|
+ def test_end_to_end_with_rotation(self) -> None:
|
|
|
350
|
+ image = _create_test_image(200, 400)
|
|
|
351
|
+ path = _image_to_path(image, ".png")
|
|
|
352
|
+ with tempfile.TemporaryDirectory() as tmp:
|
|
|
353
|
+ result = process_image(Path(path), 2, 2, Path(tmp), pre_rotate=90)
|
|
|
354
|
+ assert len(result) == 4
|
|
|
355
|
+ for p in result:
|
|
|
356
|
+ assert p.exists()
|
|
|
357
|
+ loaded = cv2.imread(str(result[0]))
|
|
|
358
|
+ assert loaded.shape[0] > 0
|
|
|
359
|
+
|
|
|
360
|
+
|
|
|
361
|
+class TestFindContentBounds:
|
|
|
362
|
+ def test_uniform_image(self) -> None:
|
|
|
363
|
+ image = _create_test_image(200, 150)
|
|
|
364
|
+ image[:, :] = (255, 255, 255)
|
|
|
365
|
+ x1, y1, x2, y2 = find_content_bounds(image)
|
|
|
366
|
+ assert (x1, y1, x2, y2) == (0, 0, 200, 150)
|
|
|
367
|
+
|
|
|
368
|
+ def test_single_dot_center(self) -> None:
|
|
|
369
|
+ image = _create_test_image(200, 150)
|
|
|
370
|
+ image[:, :] = (255, 255, 255)
|
|
|
371
|
+ image[75, 100] = (0, 0, 0)
|
|
|
372
|
+ x1, y1, x2, y2 = find_content_bounds(image)
|
|
|
373
|
+ assert abs(x1 - 100) <= 1 and abs(x2 - 100) <= 1
|
|
|
374
|
+ assert abs(y1 - 75) <= 1 and abs(y2 - 75) <= 1
|
|
|
375
|
+
|
|
|
376
|
+ def test_rectangle_content(self) -> None:
|
|
|
377
|
+ image = _create_test_image(200, 150)
|
|
|
378
|
+ image[:, :] = (255, 255, 255)
|
|
|
379
|
+ image[30:100, 50:140] = (0, 0, 0)
|
|
|
380
|
+ x1, y1, x2, y2 = find_content_bounds(image)
|
|
|
381
|
+ assert x1 == 50
|
|
|
382
|
+ assert y1 == 30
|
|
|
383
|
+ assert x2 == 139
|
|
|
384
|
+ assert y2 == 99
|
|
|
385
|
+
|
|
|
386
|
+ def test_content_at_edges(self) -> None:
|
|
|
387
|
+ image = _create_test_image(200, 150)
|
|
|
388
|
+ image[:, :] = (255, 255, 255)
|
|
|
389
|
+ image[0:10, :] = (0, 0, 0)
|
|
|
390
|
+ image[:, 0:10] = (0, 0, 0)
|
|
|
391
|
+ x1, y1, _x2, _y2 = find_content_bounds(image)
|
|
|
392
|
+ assert y1 == 0
|
|
|
393
|
+ assert x1 == 0
|
|
|
394
|
+
|
|
|
395
|
+ def test_grayscale_content(self) -> None:
|
|
|
396
|
+ image = _create_test_image(100, 100)
|
|
|
397
|
+ image[:, :] = (255, 255, 255)
|
|
|
398
|
+ image[20:30, 20:30] = (128, 128, 128)
|
|
|
399
|
+ x1, y1, x2, y2 = find_content_bounds(image)
|
|
|
400
|
+ assert x1 <= 29 and x2 >= 20
|
|
|
401
|
+ assert y1 <= 29 and y2 >= 20
|
|
|
402
|
+
|
|
|
403
|
+
|
|
|
404
|
+class TestCropToContent:
|
|
|
405
|
+ def test_crop_centered_content(self) -> None:
|
|
|
406
|
+ image = _create_test_image(200, 150)
|
|
|
407
|
+ image[:, :] = (255, 255, 255)
|
|
|
408
|
+ image[30:50, 40:60] = (0, 0, 0)
|
|
|
409
|
+ result = crop_to_content(image, border_px=10)
|
|
|
410
|
+ assert result.shape[0] >= 20
|
|
|
411
|
+ assert result.shape[1] >= 20
|
|
|
412
|
+
|
|
|
413
|
+ def test_crop_uniform_image_no_change(self) -> None:
|
|
|
414
|
+ image = _create_test_image(100, 100)
|
|
|
415
|
+ image[:, :] = (255, 255, 255)
|
|
|
416
|
+ result = crop_to_content(image, border_px=10)
|
|
|
417
|
+ assert result.shape == (100, 100, 3)
|
|
|
418
|
+
|
|
|
419
|
+ def test_border_capped_by_image_edge(self) -> None:
|
|
|
420
|
+ image = _create_test_image(100, 100)
|
|
|
421
|
+ image[:, :] = (255, 255, 255)
|
|
|
422
|
+ image[10:30, 10:30] = (0, 0, 0)
|
|
|
423
|
+ result = crop_to_content(image, border_px=200)
|
|
|
424
|
+ assert result.shape == (100, 100, 3)
|
|
|
425
|
+
|
|
|
426
|
+ def test_border_equals_distance_to_edge(self) -> None:
|
|
|
427
|
+ image = _create_test_image(100, 100)
|
|
|
428
|
+ image[:, :] = (255, 255, 255)
|
|
|
429
|
+ image[40:60, 30:70] = (0, 0, 0)
|
|
|
430
|
+ result = crop_to_content(image, border_px=50)
|
|
|
431
|
+ assert result.shape[1] <= 100
|
|
|
432
|
+ assert result.shape[0] <= 100
|
|
|
433
|
+
|
|
|
434
|
+ def test_dark_bg_no_crop_uniform(self) -> None:
|
|
|
435
|
+ image = _create_test_image(100, 100)
|
|
|
436
|
+ image[:, :] = (0, 0, 0)
|
|
|
437
|
+ result = crop_to_content(image, border_px=0)
|
|
|
438
|
+ assert result.shape == (100, 100, 3)
|
|
|
439
|
+
|
|
|
440
|
+ def test_zero_border_exact_crop(self) -> None:
|
|
|
441
|
+ image = _create_test_image(200, 150)
|
|
|
442
|
+ image[:, :] = (255, 255, 255)
|
|
|
443
|
+ image[20:100, 30:170] = (0, 0, 0)
|
|
|
444
|
+ result = crop_to_content(image, border_px=0)
|
|
|
445
|
+ assert result.shape == (80, 140, 3)
|
|
|
446
|
+
|
|
|
447
|
+
|
|
|
448
|
+class TestProcessImagePostCrop:
|
|
|
449
|
+ def test_post_crop_reduces_size(self) -> None:
|
|
|
450
|
+ image = _create_test_image(400, 400)
|
|
|
451
|
+ image[:, :] = (255, 255, 255)
|
|
|
452
|
+ image[100:200, 100:200] = (0, 0, 0)
|
|
|
453
|
+ path = _image_to_path(image, ".png")
|
|
|
454
|
+ with tempfile.TemporaryDirectory() as tmp:
|
|
|
455
|
+ result = process_image(Path(path), 1, 1, Path(tmp), border_px=30, post_crop=True)
|
|
|
456
|
+ assert len(result) == 1
|
|
|
457
|
+ loaded = cv2.imread(str(result[0]))
|
|
|
458
|
+ assert loaded.shape[0] < 400 or loaded.shape[1] < 400
|
|
|
459
|
+
|
|
|
460
|
+ def test_post_crop_off_no_crop(self) -> None:
|
|
|
461
|
+ image = _create_test_image(400, 300)
|
|
|
462
|
+ path = _image_to_path(image, ".png")
|
|
|
463
|
+ with tempfile.TemporaryDirectory() as tmp:
|
|
|
464
|
+ result = process_image(Path(path), 1, 1, Path(tmp), post_crop=False)
|
|
|
465
|
+ assert len(result) == 1
|
|
|
466
|
+ loaded = cv2.imread(str(result[0]))
|
|
|
467
|
+ assert loaded.shape == (400, 500, 3)
|
|
|
468
|
+
|
|
|
469
|
+ def test_post_crop_e2e_2x2(self) -> None:
|
|
|
470
|
+ image = _create_test_image(400, 400)
|
|
|
471
|
+ image[:, :] = (255, 255, 255)
|
|
|
472
|
+ image[50:150, 50:150] = (0, 0, 0)
|
|
|
473
|
+ image[50:150, 250:350] = (0, 0, 0)
|
|
|
474
|
+ image[250:350, 50:150] = (0, 0, 0)
|
|
|
475
|
+ image[250:350, 250:350] = (0, 0, 0)
|
|
|
476
|
+ path = _image_to_path(image, ".png")
|
|
|
477
|
+ with tempfile.TemporaryDirectory() as tmp:
|
|
|
478
|
+ result_crop = process_image(Path(path), 2, 2, Path(tmp) / "crop", border_px=30, post_crop=True)
|
|
|
479
|
+ result_no = process_image(Path(path), 2, 2, Path(tmp) / "no", border_px=30, post_crop=False)
|
|
|
480
|
+ assert len(result_crop) == 4
|
|
|
481
|
+ for cp, np in zip(result_crop, result_no):
|
|
|
482
|
+ c = cv2.imread(str(cp))
|
|
|
483
|
+ n = cv2.imread(str(np))
|
|
|
484
|
+ assert c.shape[0] < n.shape[0]
|
|
|
485
|
+ assert c.shape[1] < n.shape[1]
|