Spaces:
Build error
Build error
| # SPDX-FileCopyrightText: 2026 Team Centurions | |
| # SPDX-License-Identifier: AGPL-3.0-or-later | |
| from pathlib import Path | |
| import cv2 | |
| import numpy as np | |
| import pytest | |
| from PIL import Image | |
| from pageparse.ingest import IMAGE_EXTENSIONS, load_image | |
| def test_load_image_jpg(tmp_path: Path): | |
| img_path = tmp_path / "test.jpg" | |
| synthetic = np.random.randint(0, 256, (100, 200, 3), dtype=np.uint8) | |
| cv2.imwrite(str(img_path), synthetic) | |
| result = load_image(img_path) | |
| assert result is not None | |
| assert result.shape == (100, 200, 3) | |
| def test_load_image_png_grayscale(tmp_path: Path): | |
| img_path = tmp_path / "test.png" | |
| gray = np.random.randint(0, 256, (50, 50), dtype=np.uint8) | |
| Image.fromarray(gray).save(img_path) | |
| result = load_image(img_path) | |
| assert result.ndim == 3 | |
| def test_load_image_png_rgba(tmp_path: Path): | |
| img_path = tmp_path / "test.png" | |
| rgba = np.random.randint(0, 256, (50, 50, 4), dtype=np.uint8) | |
| Image.fromarray(rgba, "RGBA").save(img_path) | |
| result = load_image(img_path) | |
| assert result.shape[-1] == 3 | |
| def test_load_image_bmp(tmp_path: Path): | |
| img_path = tmp_path / "test.bmp" | |
| rgb = np.random.randint(0, 256, (30, 40, 3), dtype=np.uint8) | |
| Image.fromarray(rgb).save(img_path) | |
| result = load_image(img_path) | |
| assert result.shape == (30, 40, 3) | |
| def test_load_image_webp(tmp_path: Path): | |
| img_path = tmp_path / "test.webp" | |
| rgb = np.random.randint(0, 256, (20, 20, 3), dtype=np.uint8) | |
| Image.fromarray(rgb).save(img_path, format="WEBP") | |
| result = load_image(img_path) | |
| assert result is not None | |
| def test_load_image_not_found(): | |
| with pytest.raises(FileNotFoundError): | |
| load_image("nonexistent.jpg") | |
| def test_load_image_invalid_file(tmp_path: Path): | |
| bad = tmp_path / "bad.jpg" | |
| bad.write_text("not an image") | |
| with pytest.raises((ValueError, OSError)): | |
| load_image(bad) | |
| def test_image_extensions_set(): | |
| assert ".jpg" in IMAGE_EXTENSIONS | |
| assert ".png" in IMAGE_EXTENSIONS | |
| assert ".bmp" in IMAGE_EXTENSIONS | |
| assert ".webp" in IMAGE_EXTENSIONS | |
| assert ".tiff" in IMAGE_EXTENSIONS | |
| assert ".pdf" not in IMAGE_EXTENSIONS | |