Files
OnlyFrames/tests/test_analyzer.py
2026-04-07 13:34:46 +02:00

112 lines
3.1 KiB
Python

import pytest
from pathlib import Path
from analyzer import is_blurry
def make_test_image(tmp_path, color=(200, 200, 200)):
from PIL import Image
img = Image.new("RGB", (100, 100), color=color)
p = tmp_path / "test.jpg"
img.save(p)
return str(p)
def test_solid_color_image_is_blurry(tmp_path):
path = make_test_image(tmp_path)
assert is_blurry(path, threshold=100) is True
def test_normal_image_is_not_blurry(tmp_path):
from PIL import Image, ImageDraw
img = Image.new("RGB", (100, 100), color=(255, 255, 255))
draw = ImageDraw.Draw(img)
for i in range(0, 100, 2):
draw.line([(i, 0), (i, 100)], fill=(0, 0, 0), width=1)
p = tmp_path / "sharp.jpg"
img.save(p)
assert is_blurry(str(p), threshold=100) is False
from analyzer import is_overexposed, is_underexposed
def test_white_image_is_overexposed(tmp_path):
path = make_test_image(tmp_path, color=(255, 255, 255))
assert is_overexposed(path, threshold=240) is True
def test_dark_image_is_underexposed(tmp_path):
path = make_test_image(tmp_path, color=(10, 10, 10))
assert is_underexposed(path, threshold=30) is True
def test_normal_image_is_neither(tmp_path):
path = make_test_image(tmp_path, color=(128, 128, 128))
assert is_overexposed(path, threshold=240) is False
assert is_underexposed(path, threshold=30) is False
from analyzer import find_duplicates
def make_gradient_image(tmp_path, name="gradient.jpg"):
from PIL import Image
img = Image.new("L", (100, 100))
pixels = img.load()
for y in range(100):
for x in range(100):
pixels[x, y] = x * 255 // 100
p = tmp_path / name
img.convert("RGB").save(p)
return str(p)
def make_diagonal_image(tmp_path, name="diagonal.jpg"):
from PIL import Image
img = Image.new("L", (100, 100))
pixels = img.load()
for y in range(100):
for x in range(100):
pixels[x, y] = 255 if x > y else 0
p = tmp_path / name
img.convert("RGB").save(p)
return str(p)
def test_identical_images_are_duplicates(tmp_path):
p1 = make_gradient_image(tmp_path, "img1.jpg")
import shutil
p2 = tmp_path / "copy.jpg"
shutil.copy(p1, p2)
groups = find_duplicates([p1, str(p2)], threshold=8)
assert len(groups) == 1
assert len(groups[0]) == 2
def test_different_images_are_not_duplicates(tmp_path):
p1 = make_gradient_image(tmp_path, "gradient.jpg")
p2 = make_diagonal_image(tmp_path, "diagonal.jpg")
groups = find_duplicates([p1, p2], threshold=8)
assert len(groups) == 0
from analyzer import analyze_folder
def test_analyze_folder_returns_results(tmp_path):
make_test_image(tmp_path, color=(128, 128, 128))
from PIL import Image
white = tmp_path / "white.jpg"
Image.new("RGB", (100, 100), color=(255, 255, 255)).save(white)
results = analyze_folder(
folder=str(tmp_path),
blur_threshold=100,
over_threshold=240,
under_threshold=30,
dup_threshold=8,
use_ai=False,
)
reasons_flat = [r for item in results for r in item["reasons"]]
assert "ueberbelichtet" in reasons_flat