Spaces:
Running
Running
| import gradio as gr | |
| import numpy as np | |
| import cv2 | |
| from PIL import Image | |
| import os | |
| def enhance_image( | |
| input_img, | |
| contrast=1.2, | |
| brightness=10, | |
| sharpness=2.0, | |
| denoise_strength=10 | |
| ): | |
| """Enhance image using OpenCV operations""" | |
| try: | |
| # Convert to OpenCV format | |
| img = np.array(input_img) | |
| # Contrast and brightness adjustment | |
| img = cv2.convertScaleAbs(img, alpha=contrast, beta=brightness) | |
| # Denoising | |
| img = cv2.fastNlMeansDenoisingColored(img, None, denoise_strength, denoise_strength, 7, 21) | |
| # Sharpening | |
| kernel = np.array([[-1,-1,-1], | |
| [-1,9,-1], | |
| [-1,-1,-1]]) | |
| img = cv2.filter2D(img, -1, kernel) | |
| # Color correction | |
| img = cv2.cvtColor(img, cv2.COLOR_RGB2LAB) | |
| l, a, b = cv2.split(img) | |
| clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8)) | |
| l = clahe.apply(l) | |
| img = cv2.merge((l,a,b)) | |
| img = cv2.cvtColor(img, cv2.COLOR_LAB2RGB) | |
| return Image.fromarray(img) | |
| except Exception as e: | |
| raise gr.Error(f"Enhancement failed: {str(e)}") | |
| # Create interface with adjustable parameters | |
| demo = gr.Interface( | |
| fn=enhance_image, | |
| inputs=[ | |
| gr.Image(type="pil", label="Input Image"), | |
| gr.Slider(0.5, 2.0, value=1.2, label="Contrast"), | |
| gr.Slider(0, 30, value=10, label="Brightness"), | |
| gr.Slider(0.5, 3.0, value=2.0, label="Sharpness"), | |
| gr.Slider(0, 20, value=10, label="Denoise Strength") | |
| ], | |
| outputs=gr.Image(type="pil", label="Enhanced Image"), | |
| title="Image Enhancement Tool", | |
| allow_flagging="never", | |
| examples=["example.jpg"] if os.path.exists("example.jpg") else None | |
| ) | |
| demo.launch() |