diff --git a/analyzer.py b/analyzer.py new file mode 100644 index 0000000..fd873ad --- /dev/null +++ b/analyzer.py @@ -0,0 +1,11 @@ +import cv2 +import numpy as np + + +def is_blurry(path: str, threshold: float = 100.0) -> bool: + """Gibt True zurueck, wenn das Bild unscharf ist (Laplacian Variance < threshold).""" + img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) + if img is None: + return False + variance = cv2.Laplacian(img, cv2.CV_64F).var() + return bool(variance < threshold) diff --git a/tests/test_analyzer.py b/tests/test_analyzer.py new file mode 100644 index 0000000..c145370 --- /dev/null +++ b/tests/test_analyzer.py @@ -0,0 +1,27 @@ +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