Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,3 @@
|
|
| 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
|
|
@@ -17,73 +7,76 @@ from PIL import Image, ImageEnhance, ImageFilter
|
|
| 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(
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 44 |
-
|
| 45 |
-
|
| 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 |
-
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
return None, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, None, None
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import tempfile
|
|
|
|
| 7 |
def apply_enhancements(image, contrast, sharpness, brightness, saturation, upscale, blur_radius):
|
| 8 |
if image is None:
|
| 9 |
return None, None
|
| 10 |
+
|
| 11 |
original = Image.fromarray(image).convert("RGB")
|
| 12 |
+
|
| 13 |
+
# Prevent large image crashes
|
| 14 |
+
max_size = 1024
|
| 15 |
+
if max(original.size) > max_size:
|
| 16 |
+
original.thumbnail((max_size, max_size))
|
| 17 |
+
|
| 18 |
enhanced = original.copy()
|
| 19 |
+
|
| 20 |
if upscale != 1.0:
|
| 21 |
w, h = enhanced.size
|
| 22 |
enhanced = enhanced.resize((int(w * upscale), int(h * upscale)), Image.LANCZOS)
|
| 23 |
+
|
| 24 |
enhanced = ImageEnhance.Contrast(enhanced).enhance(contrast)
|
| 25 |
enhanced = ImageEnhance.Brightness(enhanced).enhance(brightness)
|
| 26 |
enhanced = ImageEnhance.Color(enhanced).enhance(saturation)
|
| 27 |
enhanced = ImageEnhance.Sharpness(enhanced).enhance(sharpness)
|
| 28 |
+
|
| 29 |
if blur_radius > 0:
|
| 30 |
enhanced = enhanced.filter(ImageFilter.GaussianBlur(radius=blur_radius))
|
| 31 |
+
|
| 32 |
return np.array(original), np.array(enhanced)
|
| 33 |
|
| 34 |
|
| 35 |
def save_enhanced(image, contrast, sharpness, brightness, saturation, upscale, blur_radius):
|
| 36 |
+
_, enh_np = apply_enhancements(
|
| 37 |
+
image, contrast, sharpness, brightness, saturation, upscale, blur_radius
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
if enh_np is None:
|
| 41 |
return None
|
| 42 |
+
|
| 43 |
tmp = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
| 44 |
+
tmp.close()
|
| 45 |
+
|
| 46 |
Image.fromarray(enh_np).save(tmp.name)
|
| 47 |
return tmp.name
|
| 48 |
|
| 49 |
|
| 50 |
+
with gr.Blocks(title="Image Enhancement Lab") as demo:
|
| 51 |
+
gr.Markdown("# Image Enhancement Lab")
|
| 52 |
+
gr.Markdown("Upload an image and adjust sliders.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
with gr.Row():
|
| 55 |
+
with gr.Column():
|
| 56 |
+
image_input = gr.Image(type="numpy")
|
| 57 |
|
| 58 |
+
contrast = gr.Slider(0.1, 3.0, 1.0)
|
| 59 |
+
brightness = gr.Slider(0.1, 3.0, 1.0)
|
| 60 |
+
saturation = gr.Slider(0.0, 3.0, 1.0)
|
| 61 |
+
sharpness = gr.Slider(0.0, 5.0, 1.0)
|
| 62 |
+
upscale = gr.Slider(0.25, 4.0, 1.0)
|
| 63 |
+
blur = gr.Slider(0.0, 10.0, 0.0)
|
| 64 |
|
| 65 |
+
apply_btn = gr.Button("Apply")
|
| 66 |
+
save_btn = gr.Button("Download")
|
| 67 |
|
| 68 |
+
download = gr.File(visible=True)
|
|
|
|
| 69 |
|
| 70 |
+
with gr.Column():
|
| 71 |
+
original_out = gr.Image()
|
| 72 |
+
enhanced_out = gr.Image()
|
| 73 |
|
| 74 |
+
inputs = [image_input, contrast, sharpness, brightness, saturation, upscale, blur]
|
| 75 |
|
| 76 |
+
apply_btn.click(apply_enhancements, inputs, [original_out, enhanced_out])
|
| 77 |
+
save_btn.click(save_enhanced, inputs, download)
|
| 78 |
|
| 79 |
|
| 80 |
+
# 🚀 REQUIRED for Spaces
|
| 81 |
+
demo.queue()
|
| 82 |
+
demo.launch()
|