Example2 / app.py
Sangwon78's picture
Update app.py
346e70b verified
Raw History Blame
2.51 kB
import gradio as gr
import cv2
import numpy as np
from PIL import Image
import io
# ํ•„ํ„ฐ ์ ์šฉ ํ•จ์ˆ˜
def apply_filter(image, filter_type, intensity):
image = np.array(image)
if filter_type == "Soft Glow":
blur = cv2.GaussianBlur(image, (15, 15), intensity)
image = cv2.addWeighted(image, 1.0, blur, 0.5, 0)
elif filter_type == "Portrait Enhancer":
image = cv2.detailEnhance(image, sigma_s=10, sigma_r=0.15 * intensity)
elif filter_type == "Warm Tone":
image = cv2.transform(image, np.array([[[1.1, 0.95, 0.8]]]) * intensity)
elif filter_type == "Cold Tone":
image = cv2.transform(image, np.array([[[0.9, 1.1, 1.2]]]) * intensity)
elif filter_type == "High-Key":
image = cv2.add(image, np.array([intensity * 50.0]))
elif filter_type == "Low-Key":
image = cv2.add(image, np.array([-intensity * 50.0]))
elif filter_type == "Haze":
haze = cv2.GaussianBlur(image, (int(5 * intensity), int(5 * intensity)), 0)
image = cv2.addWeighted(image, 0.5, haze, 0.5, 0)
elif filter_type == "Monochrome":
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
return image
# Gradio ์ธํ„ฐํŽ˜์ด์Šค
def process_image(image, filter_type, intensity):
filtered_image = apply_filter(image, filter_type, intensity)
return filtered_image
def save_image(image, file_format):
image = Image.fromarray(image)
buf = io.BytesIO()
image.save(buf, format=file_format)
buf.seek(0)
return buf
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
input_image = gr.Image(label="์—…๋กœ๋“œ ์ด๋ฏธ์ง€", type="numpy")
filter_choice = gr.Dropdown(
choices=["Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone", "High-Key", "Low-Key", "Haze", "Monochrome"],
value="Soft Glow", label="ํ•„ํ„ฐ ์„ ํƒ"
)
intensity_slider = gr.Slider(0.1, 2.0, value=1.0, label="ํ•„ํ„ฐ ๊ฐ•๋„")
format_dropdown = gr.Dropdown(choices=["JPEG", "PNG", "PDF"], label="์ €์žฅ ํ˜•์‹")
save_button = gr.Button("๋‹ค์šด๋กœ๋“œ")
with gr.Column():
output_image = gr.Image(label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง€", type="numpy")
input_image.change(process_image, [input_image, filter_choice, intensity_slider], output_image)
save_button.click(save_image, [output_image, format_dropdown], outputs=gr.File(label="ํŒŒ์ผ ์ €์žฅ"))
demo.launch()