|
|
""" |
|
|
Simple color-preserving enhancement without AI models |
|
|
""" |
|
|
|
|
|
import cv2 |
|
|
import numpy as np |
|
|
import os |
|
|
from PIL import Image, ImageEnhance |
|
|
|
|
|
class SimpleColorEnhancer: |
|
|
"""Simple enhancement that preserves colors""" |
|
|
|
|
|
def enhance_batch(self, frames_dir: str): |
|
|
"""Enhance all frames in directory""" |
|
|
frame_files = sorted([f for f in os.listdir(frames_dir) if f.endswith('.png')]) |
|
|
|
|
|
print(f"🎨 Enhancing {len(frame_files)} frames with color preservation...") |
|
|
|
|
|
for i, frame_file in enumerate(frame_files): |
|
|
frame_path = os.path.join(frames_dir, frame_file) |
|
|
|
|
|
|
|
|
is_last = (i == len(frame_files) - 1) |
|
|
if is_last: |
|
|
print(f" ⚠️ Processing last frame {frame_file} with extra care...") |
|
|
|
|
|
self.enhance_single(frame_path, frame_path, skip_if_last=is_last) |
|
|
|
|
|
if (i + 1) % 10 == 0: |
|
|
print(f" Progress: {i+1}/{len(frame_files)} frames") |
|
|
|
|
|
print("✅ Color enhancement complete") |
|
|
|
|
|
def enhance_single(self, input_path: str, output_path: str, skip_if_last: bool = False): |
|
|
"""Enhance single image with color preservation""" |
|
|
try: |
|
|
|
|
|
img = cv2.imread(input_path) |
|
|
if img is None: |
|
|
return |
|
|
|
|
|
|
|
|
if skip_if_last: |
|
|
print(f" Skipping enhancement for last frame to avoid color issues") |
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
img = cv2.bilateralFilter(img, 5, 30, 30) |
|
|
|
|
|
|
|
|
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) |
|
|
img_pil = Image.fromarray(img_rgb) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
contrast = ImageEnhance.Contrast(img_pil) |
|
|
img_pil = contrast.enhance(1.05) |
|
|
|
|
|
|
|
|
color = ImageEnhance.Color(img_pil) |
|
|
img_pil = color.enhance(1.05) |
|
|
|
|
|
|
|
|
img_enhanced = cv2.cvtColor(np.array(img_pil), cv2.COLOR_RGB2BGR) |
|
|
|
|
|
|
|
|
kernel = np.array([[0, -0.25, 0], |
|
|
[-0.25, 2, -0.25], |
|
|
[0, -0.25, 0]], dtype=np.float32) |
|
|
img_enhanced = cv2.filter2D(img_enhanced, -1, kernel) |
|
|
|
|
|
|
|
|
img_enhanced = np.clip(img_enhanced, 0, 255).astype(np.uint8) |
|
|
|
|
|
|
|
|
cv2.imwrite(output_path, img_enhanced, [cv2.IMWRITE_PNG_COMPRESSION, 1]) |
|
|
|
|
|
except Exception as e: |
|
|
print(f"⚠️ Enhancement failed for {os.path.basename(input_path)}: {e}") |