Add photo_stitcher/src/photo_stitcher/core.py
This commit is contained in:
parent
5224bdb7a7
commit
7d87c67008
1 changed files with 66 additions and 0 deletions
66
photo_stitcher/src/photo_stitcher/core.py
Normal file
66
photo_stitcher/src/photo_stitcher/core.py
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import List
|
||||||
|
from PIL import Image
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def combine_images(image_list: List[str], output_file: str) -> None:
|
||||||
|
"""Kombiniert mehrere Bilder zu einem Gesamtbild auf Basis des Pixel-Mittelwerts.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
image_list (List[str]): Liste von Pfaden zu den Eingabebildern.
|
||||||
|
output_file (str): Pfad der Ausgabedatei, in der das kombinierte Bild gespeichert wird.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: Wenn image_list leer ist oder eine Datei nicht existiert.
|
||||||
|
OSError: Falls ein Bild nicht geöffnet werden kann.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Eingabevalidierung
|
||||||
|
if not image_list:
|
||||||
|
raise ValueError("Die Bildliste darf nicht leer sein.")
|
||||||
|
|
||||||
|
resolved_paths = []
|
||||||
|
for path_str in image_list:
|
||||||
|
p = Path(path_str)
|
||||||
|
if not p.exists():
|
||||||
|
raise ValueError(f"Eingabedatei nicht gefunden: {path_str}")
|
||||||
|
resolved_paths.append(p)
|
||||||
|
|
||||||
|
# Bilder laden
|
||||||
|
images = []
|
||||||
|
for p in resolved_paths:
|
||||||
|
try:
|
||||||
|
img = Image.open(p).convert("RGB")
|
||||||
|
images.append(img)
|
||||||
|
except Exception as e:
|
||||||
|
raise OSError(f"Fehler beim Öffnen der Datei {p}: {e}")
|
||||||
|
|
||||||
|
# Bildergrößen aneinander anpassen (alle auf kleinste Größe)
|
||||||
|
min_width = min(img.width for img in images)
|
||||||
|
min_height = min(img.height for img in images)
|
||||||
|
|
||||||
|
resized = [img.resize((min_width, min_height)) for img in images]
|
||||||
|
|
||||||
|
# Pixelweise Mittelwert berechnen
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
np_arrays = [np.array(img, dtype=float) for img in resized]
|
||||||
|
mean_array = sum(np_arrays) / len(np_arrays)
|
||||||
|
mean_array = np.clip(mean_array, 0, 255).astype('uint8')
|
||||||
|
|
||||||
|
result = Image.fromarray(mean_array, mode='RGB')
|
||||||
|
|
||||||
|
output_path = Path(output_file)
|
||||||
|
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
try:
|
||||||
|
result.save(output_path)
|
||||||
|
except Exception as e:
|
||||||
|
raise OSError(f"Fehler beim Speichern von {output_path}: {e}")
|
||||||
|
|
||||||
|
# Speicher freigeben
|
||||||
|
for img in images:
|
||||||
|
img.close()
|
||||||
|
|
||||||
|
return None
|
||||||
Loading…
Reference in a new issue