Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,68 @@
|
|
| 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 |
-
|
|
|
|
| 10 |
if filter_type == "Soft Glow":
|
| 11 |
-
|
| 12 |
-
|
| 13 |
elif filter_type == "Portrait Enhancer":
|
| 14 |
-
|
| 15 |
elif filter_type == "Warm Tone":
|
| 16 |
-
|
| 17 |
elif filter_type == "Cold Tone":
|
| 18 |
-
|
| 19 |
elif filter_type == "High-Key":
|
| 20 |
-
|
| 21 |
elif filter_type == "Low-Key":
|
| 22 |
-
|
| 23 |
elif filter_type == "Haze":
|
| 24 |
-
|
| 25 |
-
image = cv2.addWeighted(image, 0.5, haze, 0.5, 0)
|
| 26 |
elif filter_type == "Monochrome":
|
| 27 |
-
gray = cv2.cvtColor(
|
| 28 |
-
|
| 29 |
-
return image
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
return temp_file
|
| 42 |
|
| 43 |
-
#
|
| 44 |
with gr.Blocks() as demo:
|
|
|
|
| 45 |
with gr.Row():
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageEnhance, ImageOps
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# ํํฐ ๊ธฐ๋ฅ ๊ตฌํ
|
| 7 |
+
def apply_filter(image, filter_type, intensity):
|
| 8 |
+
img = np.array(image)
|
| 9 |
+
|
| 10 |
if filter_type == "Soft Glow":
|
| 11 |
+
gaussian = cv2.GaussianBlur(img, (15, 15), 0)
|
| 12 |
+
img = cv2.addWeighted(img, 1 - intensity, gaussian, intensity, 0)
|
| 13 |
elif filter_type == "Portrait Enhancer":
|
| 14 |
+
img = cv2.bilateralFilter(img, 9, 75, 75)
|
| 15 |
elif filter_type == "Warm Tone":
|
| 16 |
+
img[:, :, 0] = cv2.add(img[:, :, 0], int(50 * intensity)) # Red ์ฆ๊ฐ
|
| 17 |
elif filter_type == "Cold Tone":
|
| 18 |
+
img[:, :, 2] = cv2.add(img[:, :, 2], int(50 * intensity)) # Blue ์ฆ๊ฐ
|
| 19 |
elif filter_type == "High-Key":
|
| 20 |
+
img = cv2.convertScaleAbs(img, alpha=1 + intensity, beta=int(30 * intensity))
|
| 21 |
elif filter_type == "Low-Key":
|
| 22 |
+
img = cv2.convertScaleAbs(img, alpha=1 - intensity, beta=int(-30 * intensity))
|
| 23 |
elif filter_type == "Haze":
|
| 24 |
+
img = cv2.addWeighted(img, 1 - intensity, np.full(img.shape, 255, dtype=np.uint8), intensity, 0)
|
|
|
|
| 25 |
elif filter_type == "Monochrome":
|
| 26 |
+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 27 |
+
img = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
|
|
|
|
| 28 |
|
| 29 |
+
return Image.fromarray(img)
|
| 30 |
+
|
| 31 |
+
# ํ์ผ ์ ์ฅ ๊ธฐ๋ฅ
|
| 32 |
+
def save_file(image, format):
|
| 33 |
+
file_path = f"processed_image.{format.lower()}"
|
| 34 |
+
image.save(file_path, format=format)
|
| 35 |
+
return file_path
|
| 36 |
|
| 37 |
+
# Gradio ์ธํฐํ์ด์ค ์ค์
|
| 38 |
+
def process_image(image, filter_type, intensity, file_format):
|
| 39 |
+
filtered_image = apply_filter(image, filter_type, intensity)
|
| 40 |
+
download_path = save_file(filtered_image, file_format)
|
| 41 |
+
return filtered_image, download_path
|
|
|
|
| 42 |
|
| 43 |
+
# UI ๊ตฌ์ฑ
|
| 44 |
with gr.Blocks() as demo:
|
| 45 |
+
gr.Markdown("# ์ด๋ฏธ์ง ํํฐ ์ ํ๋ฆฌ์ผ์ด์
")
|
| 46 |
with gr.Row():
|
| 47 |
+
image_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋")
|
| 48 |
+
filter_selector = gr.Dropdown(
|
| 49 |
+
["Soft Glow", "Portrait Enhancer", "Warm Tone", "Cold Tone",
|
| 50 |
+
"High-Key", "Low-Key", "Haze", "Monochrome"],
|
| 51 |
+
label="ํํฐ ์ ํ"
|
| 52 |
+
)
|
| 53 |
+
intensity_slider = gr.Slider(0.0, 1.0, value=0.5, label="ํํฐ ๊ฐ๋")
|
| 54 |
+
file_format_selector = gr.Radio(["JPG", "PNG", "PDF"], label="ํ์ผ ํ์ ์ ํ")
|
| 55 |
+
output_image = gr.Image(label="๊ฒฐ๊ณผ ์ด๋ฏธ์ง")
|
| 56 |
+
download_button = gr.File(label="๋ค์ด๋ก๋")
|
| 57 |
+
|
| 58 |
+
# ๋์ ์ฐ๊ฒฐ
|
| 59 |
+
submit_button = gr.Button("ํํฐ ์ ์ฉ")
|
| 60 |
+
submit_button.click(
|
| 61 |
+
process_image,
|
| 62 |
+
inputs=[image_input, filter_selector, intensity_slider, file_format_selector],
|
| 63 |
+
outputs=[output_image, download_button]
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# ์คํ
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
demo.launch()
|