Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Image Enhancement Lab — Gradio Interface
|
| 3 |
+
-----------------------------------------
|
| 4 |
+
Install dependencies:
|
| 5 |
+
pip install gradio pillow numpy
|
| 6 |
+
|
| 7 |
+
Run:
|
| 8 |
+
python image_enhancer.py
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import gradio as gr
|
| 12 |
+
import numpy as np
|
| 13 |
+
import tempfile
|
| 14 |
+
from PIL import Image, ImageEnhance, ImageFilter
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def apply_enhancements(image, contrast, sharpness, brightness, saturation, upscale, blur_radius):
|
| 18 |
+
if image is None:
|
| 19 |
+
return None, None
|
| 20 |
+
original = Image.fromarray(image).convert("RGB")
|
| 21 |
+
enhanced = original.copy()
|
| 22 |
+
if upscale != 1.0:
|
| 23 |
+
w, h = enhanced.size
|
| 24 |
+
enhanced = enhanced.resize((int(w * upscale), int(h * upscale)), Image.LANCZOS)
|
| 25 |
+
enhanced = ImageEnhance.Contrast(enhanced).enhance(contrast)
|
| 26 |
+
enhanced = ImageEnhance.Brightness(enhanced).enhance(brightness)
|
| 27 |
+
enhanced = ImageEnhance.Color(enhanced).enhance(saturation)
|
| 28 |
+
enhanced = ImageEnhance.Sharpness(enhanced).enhance(sharpness)
|
| 29 |
+
if blur_radius > 0:
|
| 30 |
+
enhanced = enhanced.filter(ImageFilter.GaussianBlur(radius=blur_radius))
|
| 31 |
+
return np.array(original), np.array(enhanced)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def save_enhanced(image, contrast, sharpness, brightness, saturation, upscale, blur_radius):
|
| 35 |
+
_, enh_np = apply_enhancements(image, contrast, sharpness, brightness, saturation, upscale, blur_radius)
|
| 36 |
+
if enh_np is None:
|
| 37 |
+
return None
|
| 38 |
+
tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
| 39 |
+
Image.fromarray(enh_np).save(tmp.name)
|
| 40 |
+
return tmp.name
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def build_app():
|
| 44 |
+
with gr.Blocks(title="Image Enhancement Lab") as demo:
|
| 45 |
+
gr.Markdown("# Image Enhancement Lab")
|
| 46 |
+
gr.Markdown("Upload an image and adjust sliders — preview updates in real time.")
|
| 47 |
+
|
| 48 |
+
with gr.Row():
|
| 49 |
+
with gr.Column(scale=1, min_width=280):
|
| 50 |
+
image_input = gr.Image(label="Upload Image", type="numpy", sources=["upload", "webcam", "clipboard"])
|
| 51 |
+
contrast_slider = gr.Slider(0.1, 3.0, value=1.0, step=0.05, label="Contrast (1.0 = original)")
|
| 52 |
+
brightness_slider = gr.Slider(0.1, 3.0, value=1.0, step=0.05, label="Brightness (1.0 = original)")
|
| 53 |
+
saturation_slider = gr.Slider(0.0, 3.0, value=1.0, step=0.05, label="Saturation (0 = grayscale)")
|
| 54 |
+
sharpness_slider = gr.Slider(0.0, 5.0, value=1.0, step=0.1, label="Sharpness (0 = blurry)")
|
| 55 |
+
upscale_slider = gr.Slider(0.25, 4.0, value=1.0, step=0.25, label="Upscale Factor")
|
| 56 |
+
blur_slider = gr.Slider(0.0, 10.0, value=0.0, step=0.25, label="Gaussian Blur Radius (0 = off)")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
reset_btn = gr.Button("Reset", variant="secondary")
|
| 60 |
+
apply_btn = gr.Button("Apply", variant="primary")
|
| 61 |
+
|
| 62 |
+
save_btn = gr.Button("Download Enhanced Image", variant="secondary")
|
| 63 |
+
download_file = gr.File(label="Enhanced image", visible=False)
|
| 64 |
+
|
| 65 |
+
with gr.Column(scale=2):
|
| 66 |
+
reference_output = gr.Image(label="Reference (original)", type="numpy", interactive=False)
|
| 67 |
+
enhanced_output = gr.Image(label="Enhanced Preview", type="numpy", interactive=False)
|
| 68 |
+
|
| 69 |
+
all_inputs = [image_input, contrast_slider, sharpness_slider, brightness_slider, saturation_slider, upscale_slider, blur_slider]
|
| 70 |
+
all_outputs = [reference_output, enhanced_output]
|
| 71 |
+
|
| 72 |
+
for ctrl in [contrast_slider, sharpness_slider, brightness_slider, saturation_slider, upscale_slider, blur_slider, image_input]:
|
| 73 |
+
ctrl.change(fn=apply_enhancements, inputs=all_inputs, outputs=all_outputs)
|
| 74 |
+
|
| 75 |
+
apply_btn.click(fn=apply_enhancements, inputs=all_inputs, outputs=all_outputs)
|
| 76 |
+
|
| 77 |
+
def reset_all():
|
| 78 |
+
return None, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, None, None
|
| 79 |
+
|
| 80 |
+
reset_btn.click(fn=reset_all, outputs=[image_input, contrast_slider, sharpness_slider, brightness_slider, saturation_slider, upscale_slider, blur_slider, reference_output, enhanced_output])
|
| 81 |
+
|
| 82 |
+
save_btn.click(fn=save_enhanced, inputs=all_inputs, outputs=download_file)
|
| 83 |
+
|
| 84 |
+
return demo
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
app = build_app()
|
| 89 |
+
app.launch(share=False, server_name="127.0.0.1", server_port=7860)
|