/
sarus
/
photo-processor
Обзор
Документация
Войти
/
sarus
/
photo-processor
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
tests/test_image_loader.py
75 строк
3 KB
otcheskiy
feat: add image loading and fixed crop
01 авг 2026, 11:52
01 авг 2026, 11:52
ef42adf
Код
Авторство
О чём код?
"""Тесты загрузки изображений и RGB-нормализации.""" from __future__ import annotations from pathlib import Path import pytest from PIL import Image from photo_processor.image_loader import ImageLoadError, load_image, to_rgb from tests.helpers import make_rgb_jpeg, make_rgba_png def test_load_regular_jpeg(tmp_path: Path) -> None: path = make_rgb_jpeg(tmp_path / "sample.jpg", size=(120, 80), color=(12, 34, 56)) loaded = load_image(path) assert loaded.original_width == 120 assert loaded.original_height == 80 assert loaded.image.mode == "RGB" pixel = loaded.image.getpixel((0, 0)) assert pixel[0] == pytest.approx(12, abs=2) assert pixel[1] == pytest.approx(34, abs=2) assert pixel[2] == pytest.approx(56, abs=2) loaded.image.close() def test_exif_orientation_transpose(tmp_path: Path) -> None: path = tmp_path / "rotated.jpg" image = Image.new("RGB", (40, 20), (0, 0, 0)) # Левая половина — белая, правая — чёрная (до EXIF-поворота). for y in range(20): for x in range(20): image.putpixel((x, y), (255, 255, 255)) exif = image.getexif() exif[274] = 6 # Orientation: Rotate 90 CW image.save(path, format="JPEG", quality=100, subsampling=0, exif=exif) loaded = load_image(path) # После transpose 90 CW: 40x20 → 20x40. assert loaded.original_width == 20 assert loaded.original_height == 40 assert loaded.image.mode == "RGB" # Белая область должна оказаться сверху. top = loaded.image.getpixel((10, 5)) bottom = loaded.image.getpixel((10, 35)) assert top[0] > 200 assert bottom[0] < 40 loaded.image.close() def test_convert_to_rgb_from_l_mode() -> None: gray = Image.new("L", (8, 8), 128) rgb = to_rgb(gray) assert rgb.mode == "RGB" assert rgb.getpixel((0, 0)) == (128, 128, 128) rgb.close() gray.close() def test_png_transparency_composited_on_white(tmp_path: Path) -> None: path = make_rgba_png(tmp_path / "alpha.png", size=(40, 40)) loaded = load_image(path) assert loaded.image.mode == "RGB" # Прозрачный угол → белый фон, не чёрный. assert loaded.image.getpixel((0, 0)) == (255, 255, 255) # Непрозрачный красный квадрат сохраняется. assert loaded.image.getpixel((20, 20)) == (255, 0, 0) loaded.image.close() def test_corrupted_file_raises(tmp_path: Path) -> None: path = tmp_path / "broken.jpg" path.write_bytes(b"not-an-image") with pytest.raises(ImageLoadError): load_image(path)