GenMake-Crystal-Engine / photo_prep.py
mhtbhatia's picture
Upload 12 files
cb789b0 verified
import os
import time
import cv2
import numpy as np
from PIL import Image
def studio_enhance_image(pil_img: Image.Image) -> Image.Image:
"""
Studio-grade enhancement specifically tuned for Subsurface Laser Engraving.
Balances shadows/highlights via CLAHE and sharpens micro-details.
"""
bgr = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGB2BGR)
lab = cv2.cvtColor(bgr, cv2.COLOR_BGR2LAB)
l_channel, a, b = cv2.split(lab)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
cl = clahe.apply(l_channel)
merged_lab = cv2.merge((cl, a, b))
enhanced_bgr = cv2.cvtColor(merged_lab, cv2.COLOR_LAB2BGR)
gaussian_blur = cv2.GaussianBlur(enhanced_bgr, (0, 0), 2.0)
unsharp_bgr = cv2.addWeighted(enhanced_bgr, 1.5, gaussian_blur, -0.5, 0)
final_rgb = cv2.cvtColor(unsharp_bgr, cv2.COLOR_BGR2RGB)
return Image.fromarray(final_rgb)
def auto_enhance_to_png(image_path, out_root="GenMakeEngineV1_Output"):
"""
Wrapper to connect the Studio SSLE Enhancer to the Gradio UI 'Enhance' button.
"""
os.makedirs(out_root, exist_ok=True)
base = os.path.splitext(os.path.basename(image_path))[0]
ts = time.strftime("%Y%m%d_%H%M%S")
out_dir = os.path.join(out_root, f"{base}_{ts}", "prep")
os.makedirs(out_dir, exist_ok=True)
orig_img = Image.open(image_path).convert("RGB")
enh_img = studio_enhance_image(orig_img)
orig_path = os.path.join(out_dir, "GenMake_Original.png")
enh_path = os.path.join(out_dir, "GenMake_Enhanced.png")
orig_img.save(orig_path)
enh_img.save(enh_path)
report = "✅ Studio SSLE Enhancement Applied Successfully."
return orig_path, enh_path, report