Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
import io
|
| 6 |
|
|
|
|
| 7 |
def apply_filter(image, filter_type, intensity):
|
| 8 |
-
image =
|
| 9 |
if filter_type == "Soft Glow":
|
| 10 |
-
|
| 11 |
-
|
| 12 |
elif filter_type == "Portrait Enhancer":
|
| 13 |
-
|
| 14 |
elif filter_type == "Warm Tone":
|
| 15 |
-
|
| 16 |
-
result = cv2.transform(image, warm_filter) * intensity
|
| 17 |
elif filter_type == "Cold Tone":
|
| 18 |
-
|
| 19 |
-
result = cv2.transform(image, cold_filter) * intensity
|
| 20 |
elif filter_type == "High-Key":
|
| 21 |
-
|
| 22 |
elif filter_type == "Low-Key":
|
| 23 |
-
|
| 24 |
elif filter_type == "Haze":
|
| 25 |
-
haze = cv2.GaussianBlur(image, (
|
| 26 |
-
|
| 27 |
elif filter_type == "Monochrome":
|
| 28 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
result = image
|
| 32 |
-
|
| 33 |
-
return cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
|
| 34 |
|
|
|
|
| 35 |
def process_image(image, filter_type, intensity):
|
| 36 |
filtered_image = apply_filter(image, filter_type, intensity)
|
| 37 |
-
return filtered_image
|
| 38 |
|
| 39 |
-
def save_image(image,
|
| 40 |
image = Image.fromarray(image)
|
| 41 |
buf = io.BytesIO()
|
| 42 |
-
image.save(buf, format=
|
| 43 |
buf.seek(0)
|
| 44 |
return buf
|
| 45 |
|
| 46 |
with gr.Blocks() as demo:
|
| 47 |
-
gr.Markdown("# μ΄λ―Έμ§ νν° μ μ© λ° λ€μ΄λ‘λ")
|
| 48 |
with gr.Row():
|
| 49 |
with gr.Column():
|
| 50 |
-
input_image = gr.Image(label="
|
| 51 |
filter_choice = gr.Dropdown(
|
| 52 |
choices=["Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze", "Monochrome"],
|
| 53 |
value="Soft Glow", label="νν° μ ν"
|
| 54 |
)
|
| 55 |
-
intensity_slider = gr.Slider(0.1, 2.0, value=1.0,
|
| 56 |
-
|
|
|
|
| 57 |
with gr.Column():
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
input_image.change(process_image, [input_image, filter_choice, intensity_slider], [comparison_slider, processed_image])
|
| 63 |
-
download_button.click(save_image, [processed_image, format_choice], "νμΌ λ€μ΄λ‘λ")
|
| 64 |
|
| 65 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
import io
|
| 6 |
|
| 7 |
+
# νν° μ μ© ν¨μ
|
| 8 |
def apply_filter(image, filter_type, intensity):
|
| 9 |
+
image = np.array(image)
|
| 10 |
if filter_type == "Soft Glow":
|
| 11 |
+
blur = cv2.GaussianBlur(image, (15, 15), intensity)
|
| 12 |
+
image = cv2.addWeighted(image, 1.0, blur, 0.5, 0)
|
| 13 |
elif filter_type == "Portrait Enhancer":
|
| 14 |
+
image = cv2.detailEnhance(image, sigma_s=10, sigma_r=0.15 * intensity)
|
| 15 |
elif filter_type == "Warm Tone":
|
| 16 |
+
image = cv2.transform(image, np.array([[[1.1, 0.95, 0.8]]]) * intensity)
|
|
|
|
| 17 |
elif filter_type == "Cold Tone":
|
| 18 |
+
image = cv2.transform(image, np.array([[[0.9, 1.1, 1.2]]]) * intensity)
|
|
|
|
| 19 |
elif filter_type == "High-Key":
|
| 20 |
+
image = cv2.add(image, np.array([intensity * 50.0]))
|
| 21 |
elif filter_type == "Low-Key":
|
| 22 |
+
image = cv2.add(image, np.array([-intensity * 50.0]))
|
| 23 |
elif filter_type == "Haze":
|
| 24 |
+
haze = cv2.GaussianBlur(image, (int(5 * intensity), int(5 * intensity)), 0)
|
| 25 |
+
image = cv2.addWeighted(image, 0.5, haze, 0.5, 0)
|
| 26 |
elif filter_type == "Monochrome":
|
| 27 |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 28 |
+
image = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
|
| 29 |
+
return image
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Gradio μΈν°νμ΄μ€
|
| 32 |
def process_image(image, filter_type, intensity):
|
| 33 |
filtered_image = apply_filter(image, filter_type, intensity)
|
| 34 |
+
return filtered_image
|
| 35 |
|
| 36 |
+
def save_image(image, file_format):
|
| 37 |
image = Image.fromarray(image)
|
| 38 |
buf = io.BytesIO()
|
| 39 |
+
image.save(buf, format=file_format)
|
| 40 |
buf.seek(0)
|
| 41 |
return buf
|
| 42 |
|
| 43 |
with gr.Blocks() as demo:
|
|
|
|
| 44 |
with gr.Row():
|
| 45 |
with gr.Column():
|
| 46 |
+
input_image = gr.Image(label="μ
λ‘λ μ΄λ―Έμ§", type="numpy")
|
| 47 |
filter_choice = gr.Dropdown(
|
| 48 |
choices=["Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze", "Monochrome"],
|
| 49 |
value="Soft Glow", label="νν° μ ν"
|
| 50 |
)
|
| 51 |
+
intensity_slider = gr.Slider(0.1, 2.0, value=1.0, label="νν° κ°λ")
|
| 52 |
+
format_dropdown = gr.Dropdown(choices=["JPEG", "PNG", "PDF"], label="μ μ₯ νμ")
|
| 53 |
+
save_button = gr.Button("λ€μ΄λ‘λ")
|
| 54 |
with gr.Column():
|
| 55 |
+
output_image = gr.Image(label="κ²°κ³Ό μ΄λ―Έμ§", type="numpy")
|
| 56 |
+
|
| 57 |
+
input_image.change(process_image, [input_image, filter_choice, intensity_slider], output_image)
|
| 58 |
+
save_button.click(save_image, [output_image, format_dropdown], outputs=gr.File(label="νμΌ μ μ₯"))
|
|
|
|
|
|
|
| 59 |
|
| 60 |
demo.launch()
|