File size: 2,165 Bytes
8c3e275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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