Files
Nele-Till-Hochzeit/test_enhancements.py

89 lines
3.5 KiB
Python

import os
import cv2
import numpy as np
from PIL import Image, ImageEnhance, ImageOps
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
INPUT_FILE = os.path.join(BASE_DIR, 'media', 'Schnappix', 'HochzeitNeleTill_2026-09-19_21-10-30_Raw.jpg')
TEST_DIR = os.path.join(BASE_DIR, 'media', 'Schnappix_Test')
if not os.path.exists(TEST_DIR):
os.makedirs(TEST_DIR)
def test_option_1_autocontrast(input_path, output_path):
"""Option 1: PIL AutoContrast + leichte Sättigung"""
try:
with Image.open(input_path) as img:
# Auto-Kontrast schneidet die extremsten 1% der Pixel ab (entfernt Grauschleier)
img_auto = ImageOps.autocontrast(img, cutoff=1)
# Nur leichte Sättigung
enhancer = ImageEnhance.Color(img_auto)
img_final = enhancer.enhance(1.2)
img_final.save(output_path, quality=95)
except Exception as e:
print(f"Fehler Option 1: {e}")
def test_option_2_clahe(input_path, output_path):
"""Option 2: OpenCV CLAHE (Adaptive Histogram Equalization) - sehr gut bei schlechtem Licht"""
try:
img = cv2.imread(input_path)
# BGR zu LAB Farbraum konvertieren
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l, a, b = cv2.split(lab)
# CLAHE (Contrast Limited Adaptive Histogram Equalization) auf den L-Kanal anwenden
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
cl = clahe.apply(l)
# Zusammenfügen
limg = cv2.merge((cl,a,b))
# Zurück zu BGR
final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
# Leichte Sättigungserhöhung via HSV
hsv = cv2.cvtColor(final, cv2.COLOR_BGR2HSV).astype(np.float32)
h, s, v = cv2.split(hsv)
s = s * 1.2
s = np.clip(s, 0, 255)
hsv = cv2.merge((h, s, v)).astype(np.uint8)
final_hsv = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imwrite(output_path, final_hsv, [cv2.IMWRITE_JPEG_QUALITY, 95])
except Exception as e:
print(f"Fehler Option 2: {e}")
def test_option_3_gamma(input_path, output_path):
"""Option 3: Gamma-Korrektur zum Aufhellen ohne Ausbrennen + Sättigung"""
try:
img = cv2.imread(input_path)
# Gamma > 1 dunkelt ab, < 1 hellt auf. Oft ist schlechtes Licht zu dunkel -> 0.8
gamma = 0.8
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255
for i in np.arange(0, 256)]).astype("uint8")
# Gamma anwenden
gamma_img = cv2.LUT(img, table)
# Sättigung via HSV
hsv = cv2.cvtColor(gamma_img, cv2.COLOR_BGR2HSV).astype(np.float32)
h, s, v = cv2.split(hsv)
s = s * 1.15
s = np.clip(s, 0, 255)
hsv = cv2.merge((h, s, v)).astype(np.uint8)
final_img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imwrite(output_path, final_img, [cv2.IMWRITE_JPEG_QUALITY, 95])
except Exception as e:
print(f"Fehler Option 3: {e}")
if __name__ == '__main__':
if os.path.exists(INPUT_FILE):
print("Erstelle Test-Varianten...")
test_option_1_autocontrast(INPUT_FILE, os.path.join(TEST_DIR, 'Option_1_AutoContrast.jpg'))
test_option_2_clahe(INPUT_FILE, os.path.join(TEST_DIR, 'Option_2_CLAHE.jpg'))
test_option_3_gamma(INPUT_FILE, os.path.join(TEST_DIR, 'Option_3_Gamma.jpg'))
print("Test-Varianten erstellt im Ordner media/Schnappix_Test/")
else:
print(f"Datei nicht gefunden: {INPUT_FILE}")